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

    }
}

结果展示

相关推荐
2401_854391081 分钟前
高效开发:SpringBoot网上租赁系统实现细节
java·spring boot·后端
Cikiss9 分钟前
微服务实战——SpringCache 整合 Redis
java·redis·后端·微服务
wxin_VXbishe10 分钟前
springboot合肥师范学院实习实训管理系统-计算机毕业设计源码31290
java·spring boot·python·spring·servlet·django·php
Cikiss11 分钟前
微服务实战——平台属性
java·数据库·后端·微服务
无敌の星仔20 分钟前
一个月学会Java 第2天 认识类与对象
java·开发语言
OEC小胖胖25 分钟前
Spring Boot + MyBatis 项目中常用注解详解(万字长篇解读)
java·spring boot·后端·spring·mybatis·web
2401_857617621 小时前
SpringBoot校园资料平台:开发与部署指南
java·spring boot·后端
quokka561 小时前
Springboot 整合 logback 日志框架
java·spring boot·logback
计算机学姐1 小时前
基于SpringBoot+Vue的在线投票系统
java·vue.js·spring boot·后端·学习·intellij-idea·mybatis
救救孩子把1 小时前
深入理解 Java 对象的内存布局
java