[Java] 纯文本查看 复制代码
abstract class Animal {
private int age;
private String color;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Animal(int age, String color) {
this.age = age;
this.color = color;
}
public Animal() {
}
public abstract void eat(String something);
}
class Cat extends Animal {
@Override
public void eat(String something) {
System.out.println("猫吃" + something + "!");
}
public void catchMouse(){
System.out.println("猫在抓老鼠!");
}
}
class Dog extends Animal {
@Override
public void eat(String something) {
System.out.println("狗吃" + something + "!");
}
public void lookHome(){
System.out.println("狗子在看家!");
}
}
class Person {
private String name;
private int age;
public void keepPet(Animal animal,String something){
System.out.print(getName() + "在喂");
animal.eat(something);
}
public Person() {
}
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 Person(String name, int age) {
this.name = name;
this.age = age;
}
}
public class Demo {
public static void main(String[] args) {
Person p1 = new Person("Jack",24);
Cat cat = new Cat();
Dog dog = new Dog();
cat.eat("鱼");
dog.eat("骨头");
cat.catchMouse();
dog.lookHome();
p1.keepPet(cat,"猫粮");
p1.keepPet(dog,"狗粮");
}
}