Java综合练习

(1)

复制代码
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("在吃"+something);
    }

}

(2)

复制代码
public  class cat extends animal{
    public cat(String 黑) {
    }

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

    @Override
    public void eat(String something) {
        System.out.println(getAge()+"岁的"+getColor()+"在吃"+something);

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

(3)

复制代码
public class dog extends animal{
    public dog() {
    }

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

    @Override
    public  void eat(String something) {
        System.out.println(getAge()+"岁的"+getColor()+"颜色的狗在抱住"+something+"吃骨头");
    }
    public void lookhome(){
        System.out.println("狗在看家");
    }
}

(4)

复制代码
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 keeppet(dog dog,String something){
        System.out.println("年龄为"+age+"岁的"+name+"养了一只"+dog.getColor()+"颜色的"+dog.getAge()+"岁的狗");
        dog.eat(something);
    }
    public void keeppet(cat cat,String something){
        System.out.println("年龄为"+age+"岁的"+name+"养了一只"+cat.getColor()+"颜色的"+cat.getAge()+"岁的猫");
        cat.eat(something);
    }*/
    public void keeppet(animal a,String something){
        if(a instanceof dog d){
            System.out.println("年龄为"+age+"岁的"+name+"养了一只"+a.getColor()+"颜色的"+a.getAge()+"岁的狗");
            d.eat(something);
        } else if (a instanceof cat c) {

            System.out.println("年龄为"+age+"岁的"+name+"养了一只"+c.getColor()+"颜色的"+c.getAge()+"岁的猫");
            c.eat(something);
        }else {
            System.out.println("没有这种动物");
        }
    }
}

(5)

复制代码
public class test {
    /*public static void main(String[] args) {
        person l=new person("liujiawei",18);
        dog d=new dog("白",2);
        l.keeppet(d,"骨头");
        System.out.println();
            person n=new person("nazale",18);
            cat c=new cat();
            n.keeppet(c,"鱼");
    }*/
    public static void main(String[] args) {
        person l=new person("liujiawei",18);
        dog d=new dog("白",2);
        l.keeppet(d,"骨头");
        cat c=new cat("黑",3);
        l.keeppet(c,"鱼");
    }
}
相关推荐
海威的技术博客16 分钟前
JS中的原型与原型链
开发语言·javascript·原型模式
WPG大大通24 分钟前
基于DIODES AP43781+PI3USB31531+PI3DPX1207C的USB-C PD& Video 之全功能显示器连接端口方案
c语言·开发语言·计算机外设·开发板·电源·大大通
蓝天星空30 分钟前
spring cloud gateway 3
java·spring cloud
罗政35 分钟前
PDF书籍《手写调用链监控APM系统-Java版》第9章 插件与链路的结合:Mysql插件实现
java·mysql·pdf
从以前38 分钟前
【算法题解】Bindian 山丘信号问题(E. Bindian Signaling)
开发语言·python·算法
一根稻草君41 分钟前
利用poi写一个工具类导出逐级合并的单元格的Excel(通用)
java·excel
不白兰42 分钟前
[代码随想录23回溯]回溯的组合问题+分割子串
算法
kirito学长-Java44 分钟前
springboot/ssm网上宠物店系统Java代码编写web宠物用品商城项目
java·spring boot·后端
木头没有瓜1 小时前
ruoyi 请求参数类型不匹配,参数[giftId]要求类型为:‘java.lang.Long‘,但输入值为:‘orderGiftUnionList
android·java·okhttp