this调用属性

示例:

class Person{
    private String name;
    private int age;

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

    public String getInfo(){
        return  "姓名:" + this.name +",年龄:" + this.age;
    }
}

public class ThisDemo {
    public static void main(String[] args) {
        System.out.println(new Person("张三",28).getInfo());
    }

}

 

输出:

姓名:张三,年龄:28

 

posted @ 2018-07-10 11:20  悠然现南山  阅读(142)  评论(0编辑  收藏  举报