请问老师,代码是否有改进空间,谢谢!
来源:2-10 编程练习
weixin_慕码人4149644
2019-12-25 01:23:50
package com.immoc.animal;
public class Monkey {
String name,feature;
public Monkey() {
System.out.println("我是使用无参构造产生的猴子:");
this.name="长尾猴";
this.feature="尾巴长";
}
public Monkey(String name,String feature) {
this.name=name;
this.feature=feature;
System.out.println("我是使用带参构造产生的猴子:");
}
}package com.immoc.animal;
public class MonkeyTest {
public static void main(String[] args) {
Monkey one=new Monkey();
System.out.println("名称:"+one.name);
System.out.println("特征:"+one.feature);
System.out.println("=========================");
Monkey two=new Monkey("白头叶猴","头上有白毛,喜欢吃树叶");
System.out.println("名称:"+two.name);
System.out.println("特征:"+two.feature);
}
}
1回答
好帮手慕雪
2019-12-25
Monkey中把属性name,feature私有化,然后添加对外set,get接口。main中引用对应属性的get来获取结束。如果解决了你的疑惑,请采纳,祝学习愉快~
相似问题