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,"鱼");
    }
}
相关推荐
lkbhua莱克瓦243 小时前
集合进阶8——Stream流
java·开发语言·笔记·github·stream流·学习方法·集合
骑自行车的码农3 小时前
🍂 React DOM树的构建原理和算法
javascript·算法·react.js
20岁30年经验的码农3 小时前
Java Elasticsearch 实战指南
java·开发语言·elasticsearch
okseekw3 小时前
Java 中的注释与关键字的初步学习
java
雾岛听蓝3 小时前
C++ 类和对象(一):从概念到实践,吃透类的核心基础
开发语言·c++·经验分享·笔记
luv_sw3 小时前
JavaSE-面向对象-构造器
java
okseekw3 小时前
Java 中的类型转换:结合实战代码深入解析
java
CoderYanger3 小时前
优选算法-优先级队列(堆):75.数据流中的第K大元素
java·开发语言·算法·leetcode·职场和发展·1024程序员节
希望有朝一日能如愿以偿3 小时前
力扣每日一题:能被k整除的最小整数
数据结构·算法·leetcode
Controller-Inversion3 小时前
力扣53最大字数组和
算法·leetcode·职场和发展