创建Animal类别;设置其类型属性type并添加声音方法sound();为狗和猫分别创建子类别并重写它们的声音方法;最后定义Anim
发布时间
阅读量:
阅读量
(1)【(1)建立一个名为 Animal 的动物类,该类包含用于描述动物种类的属性 type 以及实现动物发出声音功能的方法 sound();(2)基于 Animal 类分别创建 Dog(狗)类与 Cat(猫)类,这两个子类需要对父类中的 sound() 方法进行重写以实现各自独特的声音输出;(3)构建 AnimalDemo 类,在其中定义一个 Animal 类型的对象,并依次实例化 Dog 和 Cat 类的对象,再通过 Animal 对象调用其 sound() 方法来触发 Dog 和 Cat 对象的声音输出。(难度较高)class Animal{private String type;public Animal(String type){this.type=type; }publicvoid sound(){}}class Dog extends Animal{public Dog(String type){super(type);System.out.println(“我是”+type);}publicvoid sound(){System.out.println(“汪汪!”);}}class Cat extends Animal{public Cat(String type){super(type);System.out.println(“我是”+type);}publicv
全部评论 (0)
还没有任何评论哟~
