码迷,mamicode.com
首页 > 其他好文 > 详细

5.this调用成员变量、成员函数、构造函数

时间:2014-12-08 17:15:48      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   使用   sp   on   div   问题   bs   

this的两种用法:
1.this调用成员变量和成员函数
   System.out.println("my name is "+this.name)
   this代表调用这个函数的对象。
   可以用来区分成员变量和参数,必须加this
2.使用this调用构造函数
   person(Sting name,int age,String address)
   {
      this.name=name;
      this.age=age;
      this.address=address;//前面是成员变量,后面是参数
   }

 this(name,age);//调用本类中其他的构造函数,根据传入参数个数和类型决定,解决重复代码问题
 如果this调用本类其他构造函数,必须放在构造函数的第一条语句
 记住每个构造函数可以选择调用super()或this(),但是不能同时调用。




5.this调用成员变量、成员函数、构造函数

标签:style   http   color   使用   sp   on   div   问题   bs   

原文地址:http://www.cnblogs.com/fruitbolgs/p/4151299.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!