this.属性

this是属于一个具体对象的,而不是属于一个类的。

当你创建了一个对象时,this自动的给你带过来了。

this只能在类定义的方法中使用,不能在类定义的外部使用。

 

class Person

 {

  //成员变量

  int age;

  String name;

  Dog dog;  //引用类型

  public Person(Dog dog,int age, String name) //构造方法

  {

  //可读性不好

  this.age = age ;

  this.name = name;

  this.dog = dog; 

  }

 

      //显示人名字

  public void showInfo()

  {

    System.out.println("人名是:"+name);

  }

}

 

class Dog

{

  //成员变量

  int age;

  String name;

  public Dog(int age, String name)

  {

    this.age = age ;

    this.name = name;

       }

  //显示狗名的方法

  public void showInfo()

  {

  System.out.println("狗名是:"+this.name);

  }

}

 

 

 

原文地址:https://www.cnblogs.com/ansibee/p/5584855.html