1.定义狗类:属性:年龄,颜色
行为:eat(String something)(something表示吃的东西);
看家lookHome方法(无参数)
2.定义猫类:属性:年龄,颜色
行为:eat(String something)(something表示吃的东西);
抓老鼠catchMouse方法(无参数)
3.定义Person类//饲养员:属性:姓名,年龄
行为:keepPet(Dog dog,String something)方法
功能:喂养宠物狗,something表示喂养的东西
行为:keepPet(Cat cat,String something)方法
功能:喂养宠物猫,something表示喂养的东西
生成空参,有参构造,set和get方法
4.定义测试类(完成以下打印效果):
keepPet(Dog dog,String something)方法打印内容如下:
年龄为30岁的老王养了一只黑颜色的2岁的狗
2岁的黑颜色的狗两只前腿死死的抱住骨头猛吃
keepPet(Cat cat,String something)方法打印内容如下:
年龄为25岁的老李养了一只灰颜色的3岁的猫
3岁的灰颜色的猫眯着眼睛侧着头吃鱼
父类:Animal
package a03polymorphism;
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);
}
}
子类1Dog:
package a03polymorphism;
public class Dog extends Animal{
//空参构造,带全部参数的构造
public Dog() {
}
public Dog(int age, String color) {
super(age, color);
}
//行为
public void eat(String something){
System.out.println(getAge()+"岁的"+getColor()+"颜色的狗两只前腿死死的抱住"+something+"猛吃");
}
public void lookHome(){
System.out.println("狗在看家");
}
}
子类2:Cat
package a03polymorphism;
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 a03polymorphism;
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 a03polymorphism;
public class Test {
public static void main(String[] args) {
Person p1=new Person("老王",30);
Dog d=new Dog(2,"黑");
p1.keepPet(d,"骨头");
Person p2=new Person("老李",25);
Cat c=new Cat(3,"灰");
p2.keepPet(c,"鱼");
}
}
5.思考:(1)Dog和Cat都是Animal的子类,以上案例中针对不同的动物,定义了不同的keepPet 方法,过于繁琐,能否简化
(2)Dog和Cat虽然都是Animal的子类,但是都有其特有方法,能否想办法在keepPet中 调用特有方法
饲养员Person类改为:
package a03polymorphism;
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;
}
//想要一个方法能接收所有动物,包括猫,包括狗
//方法的形参可以写这些类的父类Animal
public void keepPet(Animal a, String something) {
if (a instanceof Dog d) {
System.out.println("年龄为" + age + "岁的" + name + "养了一只" + a.getColor() + "颜色的" + a.getAge() + "岁的狗");
d.eat(something);
//弊端:输出语句不能具体到猫或狗了,只能写动物,且不能调用a.lookHome();这种特有的方法,因此可以强转
} else if (a instanceof Cat c) {
System.out.println("年龄为" + age + "岁的" + name + "养了一只" + a.getColor() + "颜色的" + a.getAge() + "岁的猫");
c.eat(something);
} else {
System.out.println("没有这种动物");
}
}
}
测试类Test改为:
package a03polymorphism;
public class Test {
public static void main(String[] args) {
//创建饲养员的对象
Person p1=new Person("老王",30);
Person p2=new Person("老李",25);
Dog d=new Dog(2,"黑");
Cat c=new Cat(3,"灰");
p1.keepPet(d,"骨头");
p2.keepPet(c,"鱼");
}
}
运行结果: