题目要求
Animal类(父类)
package www.jsu.com;
public class Animal {
private int age;
private String color;
public Animal() {
}
public Animal(int age, String color) {
this.age = age;
this.color = 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 void eat(String something){
System.out.println("动物在吃"+something);
}
}
Dog类(子类)
package www.jsu.com;
public class Dog extends Animal{
public Dog() {
}
public Dog(int age, String color) {
super(age, color);
}
@Override
public void eat(String something) {
System.out.println(getAge()+"岁的"+getColor()+"颜色的狗两只腿死死的抱着"+something+"猛吃");
}
public void lookhome(){
System.out.println("狗在看家");
}
}
Cat类(子类)
package www.jsu.com;
public class Cat extends Animal {
public Cat() {
}
public Cat(int age, String color) {
super(age, color);
}
@Override
public void eat(String something) {
System.out.println(getAge()+"岁的"+getColor()+"的猫咪着眼睛侧着头吃"+something);
}
public void catchMouse(){
System.out.println("猫在抓老鼠");
}
}
Person类
package www.jsu.com;
public class Person {
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = 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 keeppet(Dog dog,String something){
System.out.println("年龄为"+age+"岁的"+name+"养了一只"+dog.getColor()+"颜色的"+dog.getAge()+"岁的狗");
dog.eat(something);
}
public void keeppet(Cat cat,String something){
System.out.println("年龄为"+age+"岁的"+name+"养了一只"+cat.getColor()+"颜色的"+cat.getAge()+"岁的狗");
cat.eat(something);
}
}
test(主函数)
package www.jsu.com;
public class Test {
public static void main(String[] args) {
Person person=new Person("李四",10);
Dog dog=new Dog(2,"黑");
person.keeppet(dog,"骨头");
}
}