12-多态的综合练习

  • 练习

    image-20221126205533510

    • 实操

      动物类

      public class Animal {
          //动物
          //颜色
         private String color;
         //年龄
         private int age;
          private String something;
          public Animal() {
          }
      
          public Animal(String color, int age,String something) {
              this.color = color;
              this.age = age;
              this.something = something;
          }
      
          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 String getSomething() {
              return something;
          }
      
          public void setSomething(String something) {
              this.something = something;
          }
      
          //行为
          public void eat(String something){
          }
      }
      
      

      猫类

      public class cat extends Animal{
          //猫
      
          public cat() {
          }
      
          public cat(String color, int age, String something) {
              super(color, age, something);
          }
      
          public void catchMouse(){
              System.out.println("猫在抓老鼠");
          }
      
          @Override
          public void eat(String something) {
              System.out.println(getColor()+"的"+getAge()+"岁的猫在眯着眼睛侧着吃"+getSomething());
          }
      }
      
      

      狗类

      public class dog extends Animal{
          //狗
      
          public dog() {
          }
      
          public dog(String color, int age, String something) {
              super(color, age, something);
          }
      
          public void lookHome(){
              System.out.println("狗在看家");
          }
      
          @Override
          public void eat(String something) {
              System.out.println(getAge()+"岁的"+getColor()+"的狗"+"在抱着"+getSomething()+"猛吃");
          }
      
          public void keepPet(person p) {
      
          }
      }
      
      

      饲养员类

      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(Animal a,String something){
              if (a instanceof dog){
                  System.out.println(age+"岁的"+name+"养了一只"+a.getColor()+"的"+a.getAge()+"的狗");
                  a.eat(something);
                  ((dog) a).lookHome();
              }else if (a instanceof cat){
                  System.out.println(age+"岁的"+name+"养了一只"+a.getColor()+"的"+a.getAge()+"的猫");
                  a.eat(something);
                  ((cat) a).catchMouse();
              }
          }
      }
      

      测试

      public class test {
          public static void main(String[] args) {
              Animal a = new Animal();
              dog d = new dog("五颜六色",888,"泛着金光的骨头");
              d.lookHome();
              d.eat("泛着金光的骨头");
              cat c = new cat("黑白相间",999,"泛着彩光的鱼");
              c.catchMouse();
              c.eat("泛着彩光的鱼");
              person p = new person("蔡徐坤",188);
              p.keepPet(d,"泛着金光的骨头");
              p.keepPet(c,"泛着彩光的鱼");
          }
      }
      
      

posted on   allu的弟弟阿喂  阅读(22)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示