public class Animal {
private String name;
private String furColor;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFurColor() {
return furColor;
}
public void setFurColor(String furColor) {
this.furColor = furColor;
}
public Animal(String name, String furColor) {
this.name = name;
this.furColor = furColor;
}
public void enjoy(){
}
}
//子类
public class Cat extends Animal {
public Cat(String name, String furColor) {
super(name, furColor);
}
public void enjoy(){
System.out.println("名字叫"+getName()+"的猫正在喵喵叫");
}
}
public class Dog extends Animal {
public Dog(String name, String furColor) {
super(name, furColor);
}
public void enjoy(){
System.out.println("名字叫"+getName()+"的狗正在汪汪叫");
}
}
//测试类
public class Test {
public void showInfo(Animal animal){
animal.enjoy();
}
public static void main(String[] args) {
Test test=new Test();
test.showInfo(new Cat("mimi","blue"));
System.out.println("-------------------------------");
test.showInfo(new Dog("wangcai","black"));
}
}
|
|