/*
this 关键字
this指代当前new的对象
作用:
1) 在构造器的首行通过this(实参)调用本类中其他构造器
构造器不能相互调用2) 区分同名成员与局部变量问题
默认会发生就近原则
可以通过this.调用成员,否则默认找局部
如果不存在局部与成员同名问题,指代成员的this.可以省略注意:
1.this在构造器中使用,默认指代当前new的对象
2.在成员方法中的this,默认指代调用成员方法的对象
3.this不能使用在static方法中*/
public class Class001_This {
public static void main(String[] args) {
// Person p = new Person("张三",18,true);
// p.show();
Person p = new Person("张三");
System.out.println("p--->地址--->"+p);
p.show();
}
}
class Person{
//属性
public String name;
public int age;
public boolean gender;
//构造器
public Person(){
System.out.println("-----空造器.....");
}
//带参构造
public Person(String name){
System.out.println("-----1个参数构造器.....");
this.name = name;
}
public Person(String name,int age){
System.out.println("-----2个参数构造器.....");
this.name = name;
this.age = age;
}
public Person(String name,int age,boolean gender){
this(name,age);
this.gender = gender;
System.out.println("-----3个参数构造器.....");
}
//功能
public void show(){
System.out.println("this------->"+this);
String name = "str";
System.out.println(this.name+"--->"+age+"--->"+this.gender);
}
}