首页 > 其他分享 >12-多态的综合练习

12-多态的综合练习

时间:2022-11-26 22:25:26浏览次数:54  
标签:12 String color age 练习 多态 void something public

  • 练习

    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,"泛着彩光的鱼");
          }
      }
      
      

标签:12,String,color,age,练习,多态,void,something,public
From: https://www.cnblogs.com/Alluw/p/16928473.html

相关文章

  • 11-多态的优势和弊端
    多态的优势多态的弊端问题:当转换类型不一致时怎么办解决方法:利用instanceof关键字进行判断java-jdk14新特性,将其简化小结......
  • 『题解』UVA 12676 Inverting Huffman
    题目传送门题意静态哈夫曼编码是一种主要用于文本压缩的编码算法。给定一个由\(N\)个不同字符组成的特定长度的文本,算法选择\(N\)个编码,每个不同的字符一个编码。使......
  • 实验5 继承和多态
    task4:pets.hpp1#pragmaonce2#include<iostream>3#include<string>4usingnamespacestd;56//定义基类,机器宠物类MachinePets7classMachinePe......
  • leetcode-1299-easy
    ReplaceElementswithGreatestElementonRightSideGivenanarrayarr,replaceeveryelementinthatarraywiththegreatestelementamongtheelementstoit......
  • #yyds干货盘点# LeetCode 腾讯精选练习 50 题:反转字符串中的单词 III
    题目:给定一个字符串 s ,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。 示例1:输入:s="Let'stakeLeetCodecontest"输出:"s'teLekatedoCteeL......
  • 实验五 继承和多态
    实验任务4#pragmaonce#include<iostream>#include<string>usingnamespacestd;classMachinePets{public:stringnickname;MachinePets(conststri......
  • 实验5 继承和多态
    1.实验4pets.hpp1#include<iostream>2#include<string>3usingnamespacestd;4classMachinePets{5private:6stringnickname;7......
  • docker基础练习
    练习nginx[root@localhosthome]#dockersearchnginx[root@localhosthome]#dockerpullnginx[root@localhosthome]#dockerimagesREPOSITORYTAG......
  • D110设计模式_4备忘录模式20221122
     备忘录模式(MementoPattern)保存一个对象的某个状态,以便在适当的时候恢复对象。备忘录模式属于行为型模式。客户不与备忘录类耦合,与备忘录管理类耦合。优点: 1、给用......
  • java——JDBC——JDBC各个类详解——练习
    代码:Statementstmt=null;Connectionconn=null;try{......