2.this关键字
this概念
- this 是一个关键字。
- this可以使用在实例变量方法中,也可以使用在构造方法中。
- this出现在实例方法中其实代表的是当前对象。
- this不能使用在静态方法中。
- this在内存中的位置:
- 相关代码:
/*
* file:ThisTest.java
*/
/*
* this:
* 1、this是一个关键字,全部小写
* 2、this是什么,在内存方面是这样的?
* - 一个对象一个this
* - this是一个变量,是一个引用。this保存
* 3、this只能使用在实例方法中。谁调用这实例方法,this就代表谁。
* 所以this代表的是:当前对象。时
* 4、this大部分情况是可以省略的。
*
* 5、为什么this不能使用在静态方法中??????
* this代表当前对象,静态方法中不存在当前对象。
*/
public class ThisTest01 {
public static void main(String[] args) {
Customer c1 = new Customer("张三");
c1.shopping();
Customer c2 = new Customer("李四");
c2.shopping();
ThisTest01.doSome();
}
}
//顾客类
class Customer {
// 属性
// 实例变量
String name;
// 构造方法
public Customer() {
}
public Customer(String s) {
name = s;
}
// 顾客购物的方法
// 实例方法
public void shopping() {
// 这里的this是谁?this是当前对象。
// c1调用shopping(),this就是c1
// c2调用shopping(),this就是c2
// System.out.println(this.name + "正在购物!");
// this. 是可以省略的。
// this. 省略的话,还是默认访问"当前这个对象"的name。
System.out.println(name + "正在购物!");
}
// 静态方法
public static void doSome() {
// this代表的是当前对象,而静态方法的调用不需要对象。矛盾了。
// 错误:无法从静态方法上下文中引用非静态 变量 this
// System.out.println(this);
}
}
class Student {
// 实例变量,怎么访问必须先new对象,通过"引用."访问。
String name = "zhangsan";
// 静态方法
public static void m1() {
// System.out.println(name);
// this代表的是当前对象。
// System.out.println(this.name);
// 除非你这样
// Student s = new Student();
// System.out.println(s.name);
}
}
/* file:ThisTest.java */
public class ThisTest02 {
int i = 100; // 这个i变量必须通过new对象才能访问。
static int k = 111;
public static void main(String[] args) {
// 错误:无法从静态上下文中引用非静态 变量 i
// System.out.println(i);
// 怎么访问i
ThisTest02 tt = new ThisTest02();
System.out.println(tt.i);
// 静态变量用"类名."访问。
System.out.println(ThisTest02.k);
// 类名. 能不能省略?
// 可以
System.out.println(k);
}
}
this. 省略说明
- this. 大部分情况可以省略,但是用来区分局部变量和实例变量的时候不能省略。
- 相关代码
/* file:ThisTest03.java */
/*
* 1、this可以使用在实例方法中,不能使用在经静态方法中。
* 2、this关键字大部分情况下可以省略,什么时候不能省略?
* 在实例方法中,或者构造方法中,为了区分局部变量和实例变量,
* 这种情况下:this. 是不能省略的。
*/
public class ThisTest03 {
public static void main(String[] args) {
Student s = new Student();
s.setNo(111);
s.setName("张三");
System.out.println("学号:" + s.getNo());
System.out.println("姓名:" + s.getName());
Student s2 = new Student(222, "李四");
System.out.println("学号:" + s2.getNo());
System.out.println("姓名:" + s2.getName());
}
}
class Student {
// 学号
private int no;
// 姓名
private String name;
// 无参构造方法
public Student() {
}
// public Student(int i, String s) {
// no = i;
// name = s;
// }
// 上面的构造方法也增强可读性
public Student(int no, String name) {
this.no = no;
this.name = name;
}
public int getNo(){
return no;
}
// public void setNo(int no) { // 就近原则
// no = no; // 这两个no都是局部变量no,和实例变量no没关系。
// }
public void setNo(int no) {
// no是局部变量
// this.no 是指实例变量
this.no = no; //this. 在这里的作用是区分局部变量和实例变量。
}
public String getName() {
return name;
}
// public void setName(String name) { // 就近原则
// name = name; // 这两个name都是局部变量name,和实例变量name没关系。
// }
public void setName(String name) {
this.name = name;
}
}
this()
- 这种语法只能出现在构造方法第一行,表示当前构造方法调用本类其他的构造方法,目的是为了代码复用。
- 相关代码
/* file:ThisTest.java */
/*
1、this除了可以使用在实例方法中,还可以用在构造方法中。
2、新语法:通过当前的构造方法去调用另一个本类的构造方法,可以使用以下语法格式:
this(实际参数列表);
通过一个构造方法1去调用构造方法2,可以做到代码复用。
但需要注意的是:"构造方法1”和"构造方法2”都是在同一个类当中。
3、this()这个语法作用是什么?
代码复用。
4、死记硬背:
对于this()的调用只能出现在构造方法的第一行。
*/
public class ThisTest04 {
public static void main(String[] args) {
// 调用无参构造方法
Date d1 = new Date();
d1.detail();
// 调用有参数构造方法
Date d2 = new Date(2008,8,8);
d2.detail();
}
}
/*
需求:
1、定义一个日期类,可以表示年月日信息
2、需求中要求:
如果调用无参构造方法,默认创建的日期为:1970年1月1日。
当然,除了调用无参构造方法之外,也可以调用有参的构造方法来创建日期对象。
*/
class Date { // 以后写代码都要封装,属性私有化,对外提供setter and getter
// 年
private int year;
// 月
private int month;
// 日
private int day;
// 构造方法无参
// 调用无参数构造方法,初始化的日期是固定值.
public Date() {
// 错误:对this的调用必须是构造器中的第一个语句
// System.out.println(11);
/*
this.year = 1970;
this.month = 1;
this.day = 1;
*/
this(1970, 1, 1);
}
// 构造方法有参数
public Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
// 提供一个可以打印日期的方法
public void detail() {
// System.out.println(year + "年" + month + "月" + day + "日");
System.out.println(this.year + "年" + this.month + "月" + this.day + "日");
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
}
大总结(this关键字结束)
- 程序再怎么变化,万变不离其宗,有一个固定的规律:
- 所有的实例相关的都是先创建对象,通过"引用."来访问。
- 所有静态相关的都是直接采用"类名."来访问。