java:多态练习
根据需求完成代码:1.定义狗类
属性
行为:
2.定义猫类
属性:
行为:
年龄,颜色
eat(String something)(something表示吃的东西)看家lookHome方法(无参数)
年龄,颜色
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 somethind)方法打印内容如下:年龄为30岁的老王养了一只黑颜色的2岁的狗2岁的黑颜色的狗两只前腿死死的抱住骨头猛吃keepPet(Cat cat,String somethind)方法打印内容如下:年龄为25岁的老李养了一只灰颜色的3岁的猫3岁的灰颜色的猫眯着眼睛侧着头吃鱼
5.思考:
1.Dog和Cat都是Animal的子类,以上案例中针对不同的动物,定义了不同的keepPet方法,过于繁琐,能否简化,并体会简化后的好处?
2.Doq和Cat虽然都是Animal的子类,但是都有其特有方法,能否想办法在keepPet中调用特有方法?
package 多态练习;
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()
{
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);
}
}
package 多态练习;
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("猫咪在逮老鼠");
}
}
package 多态练习;
public class Duck extends Animal{
//构造
public Duck(){}
public Duck(int age,String color){
super(age,color);
}
@Override
public void eat(String something)
{
System.out.println(getAge()+"岁的"+getColor()+"颜色的鸭子在叫");
}
public void Play()
{
System.out.println("鸭子在嘻嘻");
}
}
package 多态练习;
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 this.name;
}
public void setName() {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
/* public void KeepPet(Duck duck,String something)
{
System.out.println("年龄为"+getAge()+"的老王养了一只"+duck.getColor()+"颜色的"+duck.getAge()+"的小鸭子");
duck.eat(something);
}
public void KeepPet(Cat cat,String something)
{
System.out.println("年龄为"+getAge()+"的老李养了一只"+cat.getColor()+"颜色的"+cat.getAge()+"的猫");
cat.eat(something);
}*/
//想用一个方法接受所有的动物,包括猫,狗在内
//方法的形参:可以写这些父类Animal
public void KeepPet(Animal a, String something) {
if (a instanceof Duck b) {
System.out.println("年龄为" + age + "岁的" + name + "养了一只" + a.getColor() + "颜色的" + a.getAge() + "岁的鸭子");
b.eat(something);
} else if (a instanceof Cat c) {
System.out.println("年龄为" + age + "岁的" + name + "养了一只" + a.getColor() + "颜色的" + a.getAge() + "岁的猫");
c.eat(something);
} else {
System.out.println("没有这种动物");
}
}
}
package 多态练习;
public class test {
public static void main(String[] args) {
//创建对象并且调用方法
person person1=new person("老王",30);
person person2=new person("老李",25);
Duck duck1=new Duck(2,"黄");
Duck duck2=new Duck(3,"白");
person1.KeepPet(duck1,"米粒");
person2.KeepPet(duck2,"粉条");
person p=new person("老王",30);
Duck a=new Duck(2,"黄色");
Cat c=new Cat(3,"灰色");
p.KeepPet(a,"米");
p.KeepPet(c,"小鱼仔");
}
}
原文地址:https://blog.csdn.net/2303_81073778/article/details/143726589
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!