有什么需要改进注意的吗?

有什么需要改进注意的吗?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
public class Animal {
    //属性:kind(种类)
    private String kind;
     
    public Animal(){
             
    }    
    //创建带参(king为参数)构造函数
    public Animal(String kind) {
         
    }
    //应用封装实现对私有属性的get/set操作
 
    public String getKind() {
        return kind;
    }
 
    public void setKind(String kind) {
        this.kind = kind;
    }  
             
    //创建成员方法cry():void
    public void cry() { 
         
    }
}
 
 
public class Cat extends Animal {
    //重写父类cry()方法,输出信息为“小猫的叫声:喵喵喵~~~”
    public void cry() { 
        System.out.println("小猫的叫声:喵喵喵~~~");
    }
}
 
public class Dog extends Animal {
    //重写父类cry()方法:输出信息为“小狗的叫声:汪汪汪~~~”
    public void cry() { 
        System.out.println("小狗的叫声:汪汪汪~~~");
    }
}
 
public class Sheep extends Animal {
    //重写父类的方法cry(),要求输出信息“小羊的叫声:咩咩咩~~~”
    public void cry() { 
        System.out.println("小羊的叫声:咩咩咩~~~");
    }
}
 
 
public class Test {
 
    public static void main(String[] args) {
        Animal[] n = new Animal[5];
        for (int i = 0; i < n.length; i++) {
            int temp = (int) (Math.random() * 3);
            switch (temp) {
            case 0:
                n[i] = new Cat();
                break;
            case 1:
                n[i] = new Dog();
                break;
            case 2:
                n[i] = new Sheep();
                break;
            }
            n[i].cry();
        }
    }
 
}


正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学你好,运行贴出代码,效果没有问题哦!代码的书写没有任何语法问题哦!也可以在最后写一个循环,遍历数组调用cry()方法。继续加油~

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码