Java多态练习(2024.7.10)

动物类

java 复制代码
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);
    }

}

猫类

java 复制代码
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() + "猫在抓老鼠");
    }
}

狗类

java 复制代码
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() + "狗在看家");
    }
}

人类

java 复制代码
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("没这品种");
        }
    }
}

测试

java 复制代码
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);
    }
}
相关推荐
RainbowSea19 小时前
12. LangChain4j + 向量数据库操作详细说明
java·langchain·ai编程
RainbowSea19 小时前
11. LangChain4j + Tools(Function Calling)的使用详细说明
java·langchain·ai编程
考虑考虑1 天前
Jpa使用union all
java·spring boot·后端
用户3721574261351 天前
Java 实现 Excel 与 TXT 文本高效互转
java
浮游本尊1 天前
Java学习第22天 - 云原生与容器化
java
渣哥1 天前
原来 Java 里线程安全集合有这么多种
java
间彧1 天前
Spring Boot集成Spring Security完整指南
java
间彧1 天前
Spring Secutiy基本原理及工作流程
java
Java水解1 天前
JAVA经典面试题附答案(持续更新版)
java·后端·面试
洛小豆1 天前
在Java中,Integer.parseInt和Integer.valueOf有什么区别
java·后端·面试