Java学习笔记---多态综合练习

Java学习笔记---多态综合练习_第1张图片

package com.itheimaduotaidemo2;

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 );
    }
}
package com.itheimaduotaidemo2;

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("狗在看家");
    }
}
package com.itheimaduotaidemo2;

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 com.itheimaduotaidemo2;

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 );
    }
   /* public void KeepPet(Animal a,String something){
        if(a instanceof Dog d){
            System.out.println("年龄为"+age+"岁的"+name+"养了一只"+d.getColor() +"颜色的"+d.getAge() +"岁的狗");
            d.eat(something );
        }else if(a instanceof Cat c){
            System.out.println("年龄为"+age+"岁的"+name+"养了一只"+c.getColor() +"颜色的"+c.getAge() +"岁的猫");
            c.eat(something );
        }else{
            System.out.println("没有该类动物");
        }

    }*/

}
package com.itheimaduotaidemo2;

public class Test {
    public static void main(String[] args) {
        Person p1=new Person("老王",30);
        Dog d=new Dog(2,"黑");
        Cat c=new Cat(3,"灰");
        p1.KeepPet(d,"骨头");

        Person p2=new Person("老李",25);
        p2.KeepPet(c,"鱼");
    }
}

运行结果:

Java学习笔记---多态综合练习_第2张图片

你可能感兴趣的:(java,学习,笔记)