27.编写一个Animal类,具有属性:种类;具有功能:吃、睡。定义其子类Fish
和Dog,定义主类E,在其main方法中分别创建其对象并测试对象的特性。
package zuoye; public class Animal { private String shisan; public String getShisan() { System.out.println(shisan); return shisan; } public void setShisan(String shisan) { this.shisan = shisan; } public void eat() { System.out.println("123"); } public void sleep() { System.out.println("456"); } }
package zuoye; public class Fish extends Animal { Fish() { setShisan("鱼"); } public void eat() { System.out.println("f吃"); } public void sleep() { System.out.println("f睡"); } }
package zuoye; public class Dog extends Animal{ Dog() { setShisan("狗"); } public void eat() { System.out.println("d吃"); } public void sleep() { System.out.println("d睡"); } }
package zuoye; public class E1 { public static void main(String[] args) { Animal a =new Animal(); Fish f =new Fish(); Dog d=new Dog(); a.eat(); a.sleep(); f.getShisan(); f.eat(); f.sleep(); d.getShisan(); d.eat(); d.sleep(); } }
来源:https://www.cnblogs.com/Levi1995/p/5897994.html