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);
    }
}
相关推荐
惊鸿.Jh2 分钟前
若依自定义后端接口404踩坑记录
java·开发语言
源码获取_wx:Fegn08953 分钟前
基于springboot + vue考勤管理系统
java·开发语言·vue.js·spring boot·后端·spring·课程设计
认真敲代码的小火龙4 分钟前
【JAVA项目】基于JAVA的仓库管理系统
java·开发语言·课程设计
Code_Geo5 分钟前
JAVA大数据场景使用StreamingOutput
java·大数据·开发语言·streamingoutput
郑州光合科技余经理16 分钟前
定制开发实战:海外版外卖系统PHP全栈解决方案
java·服务器·开发语言·javascript·git·uni-app·php
sg_knight18 分钟前
Docker Engine 升级指南:保障容器安全的关键步骤
java·spring boot·安全·spring·spring cloud·docker·容器
runepic24 分钟前
Python 批量合并多个 Excel 数据(自动补 0 + 生成明细)
java·python·excel
vx_bisheyuange26 分钟前
基于SpringBoot的失物招领平台的设计与实现
java·spring boot·后端·毕业设计
顾安r28 分钟前
12.15 脚本网页 bash内建命令
java·前端·javascript·html·bash
Brookty30 分钟前
Java并发编程核心的基础知识
java·开发语言·java-ee·多线程·线程安全