2-10编程问题,使用this关键字
来源:2-9 编程练习
松柏i
2018-08-13 10:36:07
public class Test {
public static void main(String[] args) {
//调用无参构造方法实例对象
Monkey one = new Monkey();
//打印输出对象属性
System.out.println("名称:"+this.name);
System.out.println("特征:"+this.feature);
//调用带参构造方法实例对象
Monkey two = new Monkey("白头叶猴","头上有白毛,喜欢吃树叶");
//打印输出对象属性
System.out.println("名称:"+this.name);
System.out.println("特征:"+this.feature);
}
}
我知道这段代码运行是错误的,必须将this改成one和two才可以,但是为什么,我不能用this来调用Monkey类中的name和feature成员属性呢?
1回答
this指的是当前类的实例,就是你测试类Test类的实例,不是Monkey类的
相似问题