2-7编程练习
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public class Monkey {//属性:姓名(name)、特征(feature) String name="长尾猴",feature="尾巴长"; //无参的构造方法(默认初始化name和feature的属性值,属性值参考效果图) public Monkey(){ System.out.println("我是使用无参构造器产生的猴子:"); } //带参的构造方法(接收外部传入的参数,分别向 name 和 feature 赋值) public Monkey(String name,String feature){ System.out.println("我是使用有参构造器产生的猴子:"); this.name="白头叶猴"; this.feature="头上有白毛,喜欢吃树叶"; } } |
=======================================================================
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public class Test { public static void main(String[] args) { //调用无参构造方法实例对象 Monkey mokey = new Monkey(); //打印输出对象属性 System.out.println("名称:"+mokey.name); System.out.println("特征:"+mokey.feature); System.out.println("========================="); //调用带参构造方法实例对象 Monkey mokey1 = new Monkey("白头叶","头上有白毛,喜欢吃树叶"); //打印输出对象属性 System.out.println("名称:"+mokey1.name); System.out.println("特征:"+mokey1.feature);}} |
老师帮我看看我的这个练习呢?
27
收起
正在回答 回答被采纳积分+1
2回答
慕虎8754231
2017-12-26 16:23:24
public class Monkey {
//属性:姓名(name)、特征(feature)
String name,feature;
//无参的构造方法(默认初始化name和feature的属性值,属性值参考效果图)
public Monkey(){
System.out.println("我是使用无参构造器产生的猴子:");
this.name="长尾猴";
this.feature="尾巴长";
}
//带参的构造方法(接收外部传入的参数,分别向 name 和 feature 赋值)
public Monkey(String name,String feature){
System.out.println("我是使用有参构造器产生的猴子:");
this.name=name;
this.feature=feature;
}
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星