[Java] this修饰方法(混积分) →→→→→进入此内容的聊天室

来自 , 2019-05-24, 写在 Java, 查看 112 次.
URL http://www.code666.cn/view/6a5dfac4
  1. /*
  2.  * this修饰属性:
  3.  * 1.你想要访问的是属性的话,前面就加上this. 例如this.age代表的就是属性的age
  4.  * 2.当不发生重名问题(形参或者局部变量和属性重名),this.可以省略不写
  5.  * 3.当发生重名问题(形参或者局部变量和属性重名),this.不可以省略了,必须写
  6.  *   当发生重名问题(形参或者局部变量和属性重名),出现就近原则
  7.  *
  8.  */
  9. public class Student {
  10.         //属性
  11.         String name;
  12.         int age;
  13.         double height;
  14.         //方法
  15.         public void eat(){
  16.                 System.out.println(/*this.*/name);
  17.                 System.out.println(this.name);
  18.         }
  19.         public void sleep(){
  20.                 int age=20;
  21.                 System.out.println(age);//20
  22.                 System.out.println(this.age);//19
  23.         }
  24.        
  25.         //构造器
  26.         public Student(){
  27.                
  28.         }
  29.        
  30.         public Student(String name,int age,double height){
  31.                 this.name=name;
  32.                 this.age=age;
  33.                 this.height=height;
  34.         }
  35.        
  36.         public static void main(String[] args) {
  37.                 //创建对象:
  38.                 Student s=new Student("lili", 19, 150.7);
  39.                 s.eat();
  40.                 s.sleep();
  41.         }
  42. }

回复 "this修饰方法(混积分)"

这儿你可以回复上面这条便签

captcha