Java多态练习(2024.7.10)

        动物类

package KeepPets20240710;

public class Animal {
    private String color;
    private int age;

    public Animal(){}
    public Animal(String color, int age) {
        this.color = color;
        this.age = age;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void eat(String something) {
        System.out.println(this.age + "岁的" + this.color + "色动物在吃" + something);
    }

}

        猫类

package KeepPets20240710;

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

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

    public void catchMouse() {
        System.out.println(this.getAge() + "的" + this.getColor() + "猫在抓老鼠");
    }
}

        狗类

package KeepPets20240710;

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

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

    public void LookHouse() {
        System.out.println(this.getAge() + "的" + this.getColor() + "狗在看家");
    }
}

        人类

package KeepPets20240710;

public class Person {
    String name;
    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 KeepPets(Animal a, String something) {
        System.out.printf("%d岁的饲养员%s在喂养%d岁的%s%s吃%s\n", this.age, this.name, a.getAge(),
                a.getColor(), (a instanceof Dog? "狗" : "猫") , something);
        if (a instanceof Dog d) {
            d.LookHouse();
        } else if(a instanceof Cat c) {
            c.catchMouse();
        } else {
            System.out.println("没这品种");
        }
    }
}

         测试

package KeepPets20240710;
import java.util.Scanner;
public class KeepPetsTest {
    public static void main(String[] args) {
        Person p = creatPerson();
        Animal aDog = creatDog();
        Animal aCat = creatCat();
        p.KeepPets(aDog, "骨头");
        p.KeepPets(aCat, "🐟");

    }

    public static Person creatPerson() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入饲养员的姓名");
        String name = sc.next();
        System.out.println("请输入饲养员的年龄");
        int age = sc.nextInt();
        return new Person(name, age);
    }

    public static Dog creatDog() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入狗的姓名");
        String name = sc.next();
        System.out.println("请输入狗的年龄");
        int age = sc.nextInt();
        return new Dog(name, age);
    }

    public static Cat creatCat() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入猫的姓名");
        String name = sc.next();
        System.out.println("请输入猫的年龄");
        int age = sc.nextInt();
        return new Cat(name, age);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值