标签:cat info com idt 图片 sed feed pac hide
案例一:
Animal:
package text2; public class Animal { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void cry(){ System.out.println("不知道怎么叫"); } }
Cat:
package text2; public class Cat extends Animal{ public void cry(){ System.out.println("喵喵叫"); } }
Dog;
package text2; public class Dog extends Animal{ public void cry(){ System.out.println("汪汪叫"); } }
Test:
package text2; public class Tset { public static void main(String[] args){ Cat cat=new Cat(); cat.cry(); Dog dog=new Dog(); dog.cry(); Animal an=new Cat(); an.cry(); an=new Dog(); an.cry(); } }
案例二;
Animal:
package text2; public class Animal { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void cry(){ System.out.println("不知道怎么叫"); } public void eat(){ } }
Cat;
package text2; public class Cat extends Animal{ public void cry(){ System.out.println("喵喵叫"); } public void eat(){ System.out.println("猫喜欢吃鱼"); } }
Dog:
package text2; public class Dog extends Animal{ public void cry(){ System.out.println("汪汪叫"); } public void eat(){ System.out.println("狗喜欢吃骨头"); } }
Food;
package text2; public class Food { String name; public void showname(){ } }
Fish:
Bone;
package text2; public class Bone extends Food{ public void showname(){ System.out.println("我是骨头"); } }
MAster:
package text2; public class Master { //给动物喂食物 public void feed(Animal an,Food f)//灵活 { an.eat(); f.showname(); } }
Test;
package text2; public class Tset { public static void main(String[] args){ Master master=new Master(); master.feed(new Dog(), new Bone()); } }
标签:cat info com idt 图片 sed feed pac hide
原文地址:https://www.cnblogs.com/helloworld2019/p/10622555.html