Java练习

这个练习我用到了继承,多态和封装。

1.继承

Animal 类是一个抽象类,它有两个子类 Dog 和 Cat。
Dog 和 Cat 分别继承自 Animal 类,因此它们可以使用 Animal 类中定义的属性和方法,同时也可以有自己特有的属性和方法。

2.多态

在 Person 类的 keepPet 方法中,通过 instanceof 关键字判断传入的动物对象是 Dog 还是 Cat 类型,然后进行相应的操作。这就是多态 的体现,即相同的方法调用可以根据不同对象的实际类型执行不同的操作

3.封装

Animal 类中的 color 和 age 属性被设置为私有(private),只能通过公共的 getter 和 setter 方法访问和修改。

java 复制代码
package com.animal.java;

public abstract 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 abstract void eat(String str);

}
java 复制代码
package com.animal.java;

public class Dog extends Animal{
    public Dog() {
    }

    public Dog(String color, int age) {
        super(color, age);
    }
    public void lookHouse(){
        System.out.println("小狗正在看家");
    }
    @Override
    public void eat(String str) {
        System.out.println(getAge() + "岁的" + getColor() + "小狗正在猛吃" + str);
    }
}
java 复制代码
package com.animal.java;

public class Cat extends Animal{
    public Cat() {
    }

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

    public void catchMouse(){
        System.out.println( getColor() +"的猫正在抓小老鼠");
    }

    @Override
    public void eat(String str) {
        System.out.println(getAge() + "岁的" + getColor() + "小猫正在吃" + str);
    }

}
java 复制代码
package com.animal.java;

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 str){
        System.out.println("年龄为" + this.age + "岁的" + this.name + "养了一只年龄为" +dog.getAge() +"岁的" + dog.getColor() +"小狗");
        dog.eat(str);
    }
     饲养猫
    public void keepPet(Cat cat,String str){
        System.out.print("年龄为" + this.age + "岁的" + this.name + "养了一只年龄为" +cat.getAge() +"岁的" + cat.getColor() +"小猫");
        cat.catchMouse();
    }
    */

    public void keepPet(Animal animal,String str) {
        if(animal instanceof Dog){
            Dog dog = (Dog) animal;
            System.out.println("年龄为" + this.age + "岁的" + this.name + "养了一只年龄为" +dog.getAge() +"岁的" + dog.getColor() +"小狗");
            dog.eat(str);
        }
        else if(animal instanceof Cat){
            Cat cat = (Cat) animal;
            System.out.println("年龄为" + this.age + "岁的" + this.name + "养了一只年龄为" +cat.getAge() +"岁的" + cat.getColor() +"小猫");
            cat.eat(str);
        }
        else {
            System.out.println("没有这种动物");
        }
    }
}
java 复制代码
package com.animal;

import com.animal.java.Animal;
import com.animal.java.Cat;
import com.animal.java.Dog;
import com.animal.java.Person;

public class Test {
    public static void main(String[] args) {
        Person person1 = new Person("老王",30);
        Animal dog = new Dog("黑色",2);
        person1.keepPet(dog,"骨头");
        Dog dog1 = new Dog("橘色",4);
        System.out.print(dog1.getAge() + "岁的" + dog1.getColor());
        dog1.lookHouse();


        Person person2 = new Person("老李",25);
        Cat cat = new Cat("灰色",3);
        person2.keepPet(cat,"小鱼干");
        cat.catchMouse();

    }
}

结果展示

相关推荐
摇滚侠2 分钟前
基于 session 的登录认证方式,基于 token 的登录认证方式,对比
java·开发语言·intellij-idea
北国1372 分钟前
【Java】多线程输出滞后/错误解决&&线程创建方式与原理
java·开发语言
假客套3 分钟前
2026 JAVA 腾讯云人脸比对工具类,支持url或者base64进行比对
java·spring boot·腾讯云人脸比对
wfsm4 分钟前
reactive streaming
java
Coder_Boy_7 分钟前
【Java核心】JVM核心知识清单
java·开发语言·jvm
在坚持一下我可没意见11 分钟前
ideaPool论坛系统测试报告
java·spring boot·功能测试·selenium·jmeter·mybatis·压力测试
像少年啦飞驰点、18 分钟前
零基础入门 RabbitMQ:从消息队列是什么到 Spring Boot 实战收发消息
java·spring boot·微服务·消息队列·rabbitmq·异步编程
v***570021 分钟前
SpringBoot项目集成ONLYOFFICE
java·spring boot·后端
阿萨德528号23 分钟前
Spring Boot实战:从零构建企业级用户中心系统(八)- 总结与最佳实践
java·spring boot·后端