java代码----多态(尚马day08作业)

(1)

  1. 结合多态,实现以下需求:
  2. 1.乐器(Instrument)分为:钢琴(Piano)、小提琴(Violin),各种乐器的弹奏( play )方法各不相同。
    1. 编写一个测试类InstrumentTest,要求:
    2. 编写方法testPlay,对各种乐器进行弹奏测试。要依据乐器的不同,进行相应的弹奏。
    3. 在main方法中进行测试

    4. /**
       * @author: sunshine
       * @description:
       * @data: 2022/2/21  8:38
       * @version: 0.1
       * @since: jdk11
       */
      @Setter
      @Getter
      public class Instrument {
      
          private String name;
      
          public void play(){
              System.out.println(name + "正在演奏");
          }
      
          public Instrument(String name) {
              this.name = name;
          }
      
          public Instrument() {
          }
      }
      
    5. /**
       * @author: sunshine
       * @description:
       * @data: 2022/2/21  8:41
       * @version: 0.1
       * @since: jdk11
       */
      public class piano extends Instrument{
          public piano() {
              super();
          }
      
          public piano(String name) {
              super(name);
          }
      
          @Override
          public void play(){
              System.out.println(getName() + "正在演奏");
          }
      }
      
    6. /**
       * @author: sunshine
       * @description:
       * @data: 2022/2/21  8:43
       * @version: 0.1
       * @since: jdk11
       */
      public class Violin extends Instrument{
      
          public Violin() {
              super();
          }
      
          public Violin(String name) {
              super(name);
          }
      
          @Override
          public void play(){
              System.out.println(getName() + "正在演奏");
          }
      }
      
    7. /**
       * @author: sunshine
       * @description:
       * @data: 2022/2/21  8:44
       * @version: 0.1
       * @since: jdk11
       */
      public class InstrumentTest {
      
          public static void main(String[] args){
              //多态方法创建对象
              Instrument instrument = new piano("钢琴");
              testPlay(instrument);
              Instrument instrument1 = new Violin("小提琴");
              testPlay(instrument1);
          }
      
          private static void testPlay(Instrument instrument) {
              instrument.play();
          }
      
      }

(2)

  1. 4.需求:为地下城勇士游戏编写怪物模块的功能
    1. 现怪物的种类有两种:哥布林、猫妖
    2. 怪物的共有的属性有:名称、血量、攻击力、防御力
    3. 哥布林特有的属性有:狂暴度,
    4. 哥布林具有的方法有:攻击(使用石头进行攻击)和移动(缓慢移动)
    5. 猫妖具有的方法有:攻击(使用爪子攻击)和移动(跳着移动)
    6. 现需要在测试类中进行测试两只哥布林和两只猫妖的攻击和移动方法,要求将创建的怪物对象存放在数组对象中进行统一管理,循环调用每只怪物的攻击和移动的方法。
    7. 要求使用多态的思想实现怪物的攻击和移动。

    8. /**
       * @author: sunshine
       * @description:
       * @data: 2022/2/21  9:02
       * @version: 0.1
       * @since: jdk11
       */
      @Setter
      @Getter
      @NoArgsConstructor
      public class Monster{
          private String name;
          private int blood;
          private int attack;
          private int defence;
      
          public void attack(){
              System.out.println( "******进行攻击");
          }
          public void move(){
              System.out.println( "**移动");
          }
      
          public Monster(String name, int blood, int attack, int defence) {
              this.name = name;
              this.blood = blood;
              this.attack = attack;
              this.defence = defence;
          }
      }
    9. /**
       * @author: sunshine
       * @description:
       * @data: 2022/2/21  9:07
       * @version: 0.1
       * @since: jdk11
       */
      @Setter
      @Getter
      public class GeBuLin extends Monster{
          private int angry;
      
          public GeBuLin(String name, int blood, int attack, int defence, int angry) {
              super(name, blood, attack, defence);
              this.angry = angry;
          }
      
          public GeBuLin() {
              super();
          }
      
          @Override
          public void attack(){
              System.out.println(getName()+ "使用石头进行攻击进行攻击");
          }
          @Override
          public void move(){
              System.out.println(getName() + "缓慢移动");
          }
      
      }
    10. /**
       * @author: sunshine
       * @description:
       * @data: 2022/2/21  9:14
       * @version: 0.1
       * @since: jdk11
       */
      @Setter
      @Getter
      public class Cat extends Monster{
      
          public Cat() {
          }
      
          public Cat(String name,int blood,int attack,int defence) {
              //所有子类都默认调用父类无参。所以子类有参构造要用super调用父类无参构造,这样才有意义。
              super(name, blood, attack, defence);
          }
      
          @Override
          public void attack(){
              System.out.println(getName() + "使用爪子攻击");
          }
          @Override
          public void move(){
              System.out.println(getName() + "跳着移动");
          }
      
      }
      
    11. /**
       * @author: sunshine
       * @description:
       * @data: 2022/2/21  9:17
       * @version: 0.1
       * @since: jdk11
       */
      public class Test {
          public static void main(String[] args){
              //创建对象,这里用普通方法
              GeBuLin geBuLin = new GeBuLin("gebulin",5,5,5,5);
              Cat cat = new Cat("maoyao",4,4,4);
              //使用数组对象
              Monster[] monsters = new Monster[4];
              monsters[0] = geBuLin;
              monsters[1] = cat;
      
              for(Monster s:monsters){
                  if(s == null){
                      break;
                  }
                  s.attack();
                  s.move();
              }
          }
      }

你可能感兴趣的:(尚马Java代码,java,开发语言,后端)