• JAVA练习题35:多态综合训练


    此题目为多态的综合训练

    根据需求完成代码:

    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 somethind)方法打印内容如下:
      年龄为30岁的老王养了一只黑颜色的2岁的狗
      2岁的黑颜色的狗两只前腿死死的抱住骨头猛吃
      keepPet(Cat cat,String somethind)方法打印内容如下:
      年龄为25岁的老李养了一只灰颜色的3岁的猫
      3岁的灰颜色的猫眯着眼睛侧着头吃鱼

    5. 思考:
      1.Dog和Cat都是Animal的子类,以上案例中针对不同的动物,定义了不同的keepPet方法,过于繁琐,能否简化,并体会简化后的好处?
      2.Dog和Cat虽然都是Animal的子类,但是都有其特有方法,能否想办法在keepPet中调用特有方法?

    1.Animal类

    package polymorphicdemo1;
    
    public class Animal {
        private int age;    //年龄
        private String color;   //颜色
    
        public Animal() {
        }
    
        public Animal(int age, String color) {
            this.age = age;
            this.color = color;
        }
        public void eat(String something){
            System.out.println("动物在吃" + something);
        }
    
        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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    2. Dog类

    package polymorphicdemo1;
    //    1.定义狗类
    //    属性:年龄,颜色
    //    行为:
    //    eat(String something)(something表示吃的东西)
    //    看家lookHome方法(无参数)
    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("狗看家");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    3. Cat类

    package polymorphicdemo1;
    //    2.定义猫类
    //    属性:年龄,颜色
    //    行为:
    //    eat(String something)方法(something表示吃的东西)
    //    逮老鼠catchMouse方法(无参数)
    public class Cat extends Animal{
        public Cat() {
        }
    
        public Cat(int age, String color) {
            super(age, color);
        }
        public void eat(String something){
            System.out.println(getAge()+"岁的" + getColor() + "颜色的猫咪着眼睛侧着头吃" + something);
        }
        public void catchMouse(){
            System.out.println("猫逮老鼠");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    4. Person类

    package polymorphicdemo1;
    //        3.定义Person类//饲养员
    //        属性:姓名,年龄
    //        行为:
    //        keepPet(Dog dog,String something)方法
    //        功能:喂养宠物狗,something表示喂养的东西
    //        行为:
    //        keepPet(Cat cat,String something)方法
    //        功能:喂养宠物猫,something表示喂养的东西
    //        生成空参有参构造,set和get方法
    public class Person {
        private String name;
        private int age;
    
        public Person() {
        }
    
        public Person(String name, int age) {
            this.name = name;
            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);
        }*/
        public void keepPet(Animal a,String something){
            if(a instanceof Dog){
                Dog d = (Dog)a;   //将动物对象a强制转换为Dog对象,并由d接收
                System.out.println("年龄为" + age + "岁的" + name + "养了一只" + d.getColor() + "颜色的" + d.getAge() + "岁的狗");
                d.eat(something);
            }else if(a instanceof Cat){
                Cat c = (Cat)a;   //将动物对象a强制转换为Cat对象,并由c接收
                System.out.println("年龄为" + age + "岁的" + name + "养了一只" + c.getColor() + "颜色的" + c.getAge() + "岁的狗");
                c.eat(something);
            }else{
                System.out.println("没有这种动物!");
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    5. 测试类

    package polymorphicdemo1;
    
    /*    4.定义测试类(完成以下打印效果):
        keepPet(Dog dog,String somethind)方法打印内容如下:
        年龄为30岁的老王养了一只黑颜色的2岁的狗
        2岁的黑颜色的狗两只前腿死死的抱住骨头猛吃
        keepPet(Cat cat,String somethind)方法打印内容如下:
        年龄为25岁的老李养了一只灰颜色的3岁的猫
        3岁的灰颜色的猫眯着眼睛侧着头吃鱼*/
    public class Test {
        public static void main(String[] args) {
            Person p = new Person("老王",30);
            Dog d = new Dog(2,"黑");
            Cat c = new Cat(3,"灰");
            p.keepPet(d,"骨头");
            p.keepPet(c,"鱼");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
  • 相关阅读:
    LeetCode 算法:二叉树的直径 c++
    java 企业工程管理系统软件源码 自主研发 工程行业适用
    Day24 Python函数
    几个可以整蛊你朋友的Python程序
    GRU 关键词提取实例
    JVM参数设置
    手搭手Ajax经典基础案例省市联动
    Docker之nacos集群部署(详细教你搭建)
    Kafaka丢消息吗
    Oracle-单行函数大全
  • 原文地址:https://blog.csdn.net/m0_46457497/article/details/127757068