/* |
* this修饰属性: |
* 1.你想要访问的是属性的话,前面就加上this. 例如this.age代表的就是属性的age |
* 2.当不发生重名问题(形参或者局部变量和属性重名),this.可以省略不写 |
* 3.当发生重名问题(形参或者局部变量和属性重名),this.不可以省略了,必须写 |
* 当发生重名问题(形参或者局部变量和属性重名),出现就近原则 |
* |
*/ |
public class Student { |
//属性 |
String name; |
int age; |
double height; |
//方法 |
public void eat(){ |
System.out.println( /*this.*/ name); |
System.out.println( this .name); |
} |
public void sleep(){ |
int age= 20 ; |
System.out.println(age); //20 |
System.out.println( this .age); //19 |
} |
|
//构造器 |
public Student(){ |
|
} |
|
public Student(String name, int age, double height){ |
this .name=name; |
this .age=age; |
this .height=height; |
} |
|
public static void main(String[] args) { |
//创建对象: |
Student s= new Student( "lili" , 19 , 150.7 ); |
s.eat(); |
s.sleep(); |
} |
} |