首页 > 其他 > 详细

this.属性

时间:2016-06-14 19:03:58      阅读:181      评论:0      收藏:0      [点我收藏+]

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);

  }

}

 

 

 

 

this.属性

原文:http://www.cnblogs.com/ansibee/p/5584855.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!