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();

    }
}

结果展示

相关推荐
摇滚侠6 分钟前
Spring Boot 3零基础教程,Spring Boot 特性介绍,笔记02
java·spring boot·笔记
Excuse_lighttime10 分钟前
只出现一次的数字(位运算算法)
java·数据结构·算法·leetcode·eclipse
艾菜籽35 分钟前
Spring Web MVC入门补充1
java·后端·spring·mvc
失散1342 分钟前
分布式专题——44 ElasticSearch安装
java·分布式·elasticsearch·架构
MicrosoftReactor1 小时前
技术速递|使用 GitHub Copilot Agent 模式现代化 Java 项目的分步指南
java·github·copilot
ahauedu2 小时前
Spring Boot 2.7+ 中 RedisConnectionFactory Autowire 警告的深度解析
java·spring boot·后端
im_AMBER2 小时前
杂记 15
java·开发语言·算法
豆沙沙包?2 小时前
2025年--Lc182--sql(排序和分组)--Java版
java·数据库·sql
CryptoRzz3 小时前
欧美(美股、加拿大股票、墨西哥股票)股票数据接口文档
java·服务器·开发语言·数据库·区块链