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 @   悠然现南山  阅读(144)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示