package 抽象内的运用; abstract class animal{ String name; String color; public animal(String name,String color) { this.name=name; this.color=color; } public void eat() { System.out.println(name+"吃粮食"); } public abstract void run (); } class Dog extends animal{ public Dog(String name ,String color) { super(name,color); } public void run(){ System.out.println("小狗爬呀爬"); } } class Fish extends animal{ public Fish(String name,String color) { super (name,color); } public void run() { System.out.println("小鱼的尾巴摇啊摇"); } }
public class Dome4 { public static void main(String[]args) { Dog d1 = new Dog("狗蛋","黄色"); System.out.println("狗的名字是:"+d1.name); System.out.println("狗的颜色是:"+d1.color); d1.eat(); d1.run(); Fish f1 = new Fish("鲸鱼","蓝色"); System.out.println("小鱼的名字是:"+f1.name); System.out.println("小鱼的颜色是:"+f1.color); f1.eat(); f1.run(); } }