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

this对象的引用

时间:2015-08-05 20:25:04      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:java   this   this构造函数调用   


第一种用法:


class  Person
{

    private String name;
    private int age;

    Person(String name)
    {
         this.name=name;
    }
    Person(int age,String name)
    {
        this.name=name;
        this.age=age;
    }
}

this用于调用类中的私有成员变量,并进行赋值操作。 比如说:


        this.name=name;
        this.age=age;

第二种用法: this用于某类中构造函数之间的调用。


Person(int age,String name)
    {
        this(name);
        this.age=age;
    }

当执行


Person p=new Person(20,"Frank")

此条语句时, this(name)会直接调用Person(String name)构造函数,接下来才执行this.age=age;语句.


但this()的这第二种用法仅仅限于类的构造函数之间。

版权声明:本文为博主原创文章,未经博主允许不得转载。

this对象的引用

标签:java   this   this构造函数调用   

原文地址:http://blog.csdn.net/hzl9966/article/details/47302475

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