this

来源:互联网 发布:mac office 2016 破解 编辑:程序博客网 时间:2024/05/23 11:50
/*this:看上去是用于区分局部变量和成员变量同名的情况this代表它所在函数所属对象的引用:即哪个对象在调用this所在的函数,this就代表哪个对象this的应用:当定义类中功能时,该函数内部要用到调用该函数的对象时,用this表示这个对象.但凡本类功能内部使用了本类对象,都用this表示this语句:用于构造函数间互相调用this语句只能放在构造函数的第一行,因为初始化要先执行*/class Person2{private String name;private int age;Person2(){}Person2(String name){this();this.name = name;}Person2(String name, int age){//this.name = name;//Person2(name);//构造函数间的调用不能用上面形式this(name);this.age = age;//this(name);放在后面会报错}void show(){System.out.println(this.name+"..."+this.age);}}class ThisDemo{public static void main(String[] args){Person2 p = new Person2("list", 30);p.show();}}

原创粉丝点击