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

08.this的使用方法

时间:2016-08-28 18:15:00      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

  • 使用this调用成员变量和成员函数
  1. classPerson{
  2. String name;
  3. void talk(){
  4. System.out.println("my name is "+this.name);
  5. }
  6. }
 
  1. classTestA{
  2. publicstaticvoid main(String args[]){
  3. Person p1 =newPerson();
  4. p1.name ="zhangsan";
  5. Person p2 =newPerson();
  6. p2.name ="lisi";
  7. p1.talk();
  8. p2.talk();
  9. }
  10. }
 
 
  • 使用this调用构造函数
    • 对this的调用函数必须是构造函数的第一个语句
 
  1. classTestA{
  2. publicstaticvoid main(String args[]){
  3. Person p1 =newPerson("zhangsan",20,"beijing");
  4. }
  5. }
 
  1. classPerson{
  2. String name;
  3. int age;
  4. String address;
  5. Person(){
  6. System.out.println("无参数的构造函数");
  7. }
  8. Person(String name,int age){
  9. this();
  10. this.name = name;
  11. this.age = age;
  12. System.out.println("两个参数的构造函数");
  13. }
  14. Person(String name,int age,String address){
  15. this(name,age);
  16. this.address = address;
  17. System.out.println("三个参数的构造函数");
  18. }
  19. }
 





08.this的使用方法

标签:

原文地址:http://www.cnblogs.com/arroneve/p/5815429.html

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