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,"鱼");
    }
}
相关推荐
咖啡教室4 小时前
java日常开发笔记和开发问题记录
java
咖啡教室4 小时前
java练习项目记录笔记
java
鱼樱前端5 小时前
maven的基础安装和使用--mac/window版本
java·后端
RainbowSea5 小时前
6. RabbitMQ 死信队列的详细操作编写
java·消息队列·rabbitmq
RainbowSea5 小时前
5. RabbitMQ 消息队列中 Exchanges(交换机) 的详细说明
java·消息队列·rabbitmq
算AI6 小时前
人工智能+牙科:临床应用中的几个问题
人工智能·算法
我不会编程5557 小时前
Python Cookbook-5.1 对字典排序
开发语言·数据结构·python
李少兄7 小时前
Unirest:优雅的Java HTTP客户端库
java·开发语言·http
此木|西贝7 小时前
【设计模式】原型模式
java·设计模式·原型模式
可乐加.糖7 小时前
一篇关于Netty相关的梳理总结
java·后端·网络协议·netty·信息与通信