java继承/多态综合联系

创建猫狗父类对象Animal

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;
    }
}
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;
    }
}

Dog子类

public class Dog extends Animal {
    public Dog() {
    }

    public Dog(int age, String color) {
        super(age, color);
    }

    //子类Dog,eat方法重写
    @Override
    public void eat(String something) {
        System.out.println(getAge() + "岁的" + getColor() + "颜色的狗两只前腿死死的抱住" + something + "猛吃");
    }

    public void lookHome() {
        System.out.println("狗在看家");
    }
}

Cat子类

public class Cat extends Animal {
    public Cat() {
    }

    public Cat(int age, String color) {
        super(age, color);
    }

    //子类Cat,eat方法重写
    @Override
    public void eat(String something) {
        System.out.println(getAge() + "岁的" + getColor() + "颜色的猫眯着眼睛吃" + something);
    }

    public void catchMouse() {
        System.out.println("猫在抓老鼠");
    }
}

Person类

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 keePet(Animal a, String something) {
        //不确定后续创建对象类型,使用instanceof关键字进行判断
        if (a instanceof Dog d) {
            //Dog类型
            System.out.println("年龄为" + age + "岁的" + name + "养了一只" + a.getColor() + "颜色" + a.getAge() + "岁的狗");
            d.eat("骨头");
        } else if (a instanceof Cat c) {
            //Cat类型
            System.out.println("年龄为" + age + "岁的" + name + "养了一只" + a.getColor() + "颜色" + a.getAge() + "岁的猫");
            c.eat("鱼");
        }
    }
}

测试类

public class Test {
    public static void main(String[] args) {
        //创建person对象p1
        Person p1 = new Person("老王", 30);
        Animal d = new Dog(2, "黑");
        p1.keePet(d, "骨头");
        //创建person对象p2
        Person p2 = new Person("老李", 25);
        Animal c = new Cat(3, "灰");
        p2.keePet(c, "鱼");
    }
}

运行结果

年龄为30岁的老王养了一只黑颜色2岁的狗
2岁的黑颜色的狗两只前腿死死的抱住骨头猛吃
年龄为25岁的老李养了一只灰颜色3岁的猫
3岁的灰颜色的猫眯着眼睛吃鱼

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

java猩猩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值