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);
    }
}
相关推荐
FG.4 分钟前
Day22
java·面试
菜鸟的迷茫6 分钟前
Redis 缓存雪崩、穿透、击穿面试题深度解析与 Spring Boot 实战代码示例
java
珹洺17 分钟前
C++算法竞赛篇:DevC++ 如何进行debug调试
java·c++·算法
SHUIPING_YANG25 分钟前
根据用户id自动切换表查询
java·服务器·数据库
爱吃烤鸡翅的酸菜鱼37 分钟前
IDEA高效开发:Database Navigator插件安装与核心使用指南
java·开发语言·数据库·编辑器·intellij-idea·database
惊涛骇浪、43 分钟前
SpringMVC + Tomcat10
java·tomcat·springmvc
墨染点香1 小时前
LeetCode Hot100【6. Z 字形变换】
java·算法·leetcode
ldj20201 小时前
SpringBoot为什么使用new RuntimeException() 来获取调用栈?
java·spring boot·后端
超龄超能程序猿1 小时前
Spring 应用中 Swagger 2.0 迁移 OpenAPI 3.0 详解:配置、注解与实践
java·spring boot·后端·spring·spring cloud
风象南2 小时前
SpringBoot配置属性热更新的轻量级实现
java·spring boot·后端