1.定义动物类
属性:
年龄,颜色
行为:
eat(String something)方法(无具体行为,不同动物吃的方式和东西不一样,something表示吃的东西)
生成空参有参构造,set和get方法
2.定义狗类继承动物类
行为:
eat(String something)方法,看家lookHome方法(无参数)
3.定义猫类继承动物类
行为:eat(String something)方法,逮老鼠catchMouse方法(无参数)
4.定义Person类
属性:
姓名,年龄
行为:
keepPet(Dog dog,String something)方法
功能:喂养宠物狗,something表示喂养的东西
行为:
keepPet(Cat cat,String something)方法
功能:喂养宠物猫,something表示喂养的东西
生成空参有参构造,set和get方法
public class Animal {
/*
- 年龄属性*/
private int age;
/颜色属性/
private String color;
/吃方法/
public void eat(String something){
System.out.println(“正字吃”+something);
}
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;
}
@Override
public String toString() {
return "Animal{" +
"age=" + age +
", color='" + color + '\'' +
'}';
}
public class Cat extends Animal {
public void eat(String something){
}
public Cat() {
}
public Cat(int age, String color) {
super(age, color);
}
public void catMouse(){
System.out.println("小花猫正在捉老鼠");
}
public class Dog extends Animal{
public void eat(String something){
}
public void lookHome(){
System.out.println("旺财正在看家");
}
public Dog() {
}
public Dog(int age, String color) {
super(age, color);
}
}
private String name;
private int age;
public void keepPet(Dog dog,String something){
System.out.println("喂养宠物狗"+dog+something);
}
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(Cat cat, String something){
System.out.println("喂养宠物猫"+cat+something);
}
public static void main(String[] args) {
Cat cat = new Cat(3,"小花花");
cat.catMouse();
cat.eat("大便");
Dog dog = new Dog(1,"旺财");
dog.lookHome();
dog.eat("大便");
Person person = new Person();
person.keepPet(dog,"吃大便");
person.keepPet(cat,"吃大便");
}