请编码实现动物世界的继承关系……定义一个体育活动类(Sports)作为基类……编写一个程序,并满足如下要求……

          请编码实现动物世界的继承关系:

            动物(Animal)具有行为:吃(eat)、睡觉(sleep)

            动物包括:兔子(Rabbit),老虎(Tiger)

            这些动物吃的行为各不相同(兔子吃草,老虎吃肉);但睡觉的行为是一致的。

            请通过继承实现以上需求,并进行测试。

 //方式一
      // 1.定义Animal类
      class Animal {
        constructor(animal, eat, sleep) {
          this.animal = animal
          this.eat = eat
          this.sleep = sleep
        }
        show() {
          console.log(`${this.animal}的共同特性是: ${this.eat}, ${this.sleep}`)
        }
      }

      //2.定义Rabbit类,继承自Animal
      class Rabbit extends Animal {
        constructor(animal, eat, sleep, grass, meat) {
          super(animal, eat, sleep)
          this.grass = grass
          this.meat = meat
        }
        show() {
          super.show()
          console.log('兔子吃:', this.grass)
          console.log('老虎吃:', this.meat)
        }
      }

      //3.测试
      let rabbit = new Rabbit('动物', '吃', '睡觉', '草', '肉')
      rabbit.show()


//   方式二
      class Animal {
          constructor(type) {
              this.type = type;
          }
          eat() { }
          sleep() {
              console.log(`${this.type}在睡觉`)
          }
      }

      class Rabbit extends Animal {
          constructor(type) {
              super(type);
          }
          eat() {
              console.log('兔子吃草....');
          }
      }

      class Tiger extends Animal {
          constructor(type) {
              super(type)
          }
          eat() {
              console.log('老虎吃肉')
          }
      }

      let t = new Tiger('老虎');
      t.eat();
      t.sleep();

                定义一个体育活动类(Sports)作为基类,它有一个进行活动的方法play,足球(Football)和篮球(Bascketball)都是体育活动类的派生类。请编写一个方法howToPlay(Sports sp),该方法要求传递一个Sports类型的参数。该方法的作用是:

            (1)当传入的对象类型为Football时,控制台上应打印、足球是用脚踢的!

            (2)当传入的对象类型为Bascketball时,控制台上应打印、篮球是用手打的!

        //1.定义Sports类
        class Sports {
            constructor(play) {
                this.play = play;
            }
            howToPlay() {
                if (this.play == '篮球') {
                    console.log('是用手打的!');
                } if (this.play == '足球') {
                    console.log('是用脚踢的!');
                }
            }
        }
        //2.定义篮球
        class Bascketball extends Sports {
            constructor(play, bascketball) {
                super(play);
                this.ball = bascketball;
            }
            show() {
                console.log(this.ball);
                super.howToPlay();

            }
        }
        //3.定义足球
        class Football extends Sports {
            constructor(play, football) {
                super(play);
                this.ball = football;
            }
            show() {
                console.log(this.ball)
                super.howToPlay();

            }
        }
        let bascketball = new Bascketball('篮球', '篮球');
        bascketball.show();

        let football = new Football('足球', '足球');
        football.show();

         编写一个程序,并满足如下要求:

        (1)编写一个Car类,具有:属性:品牌(mark),功能:驾驶(drive( ))

        (2)定义Car类的子类SubCar,具有:属性:价格(price)、速度(speed),功能:变速(speedChange(newSpeed)),把新速度赋给speed

        class Car {
            constructor(mark, drive, speed) {
                this.mark = mark;
                this.drive = drive;
                this.speed = speed;
            }
            show() {
                console.log('本车的品牌是:', this.mark);
                console.log('本车正在:', this.drive);
                console.log(`${this.mark}原来的速度是${this.speed}`);
            }
        }


        class SubCar1 extends Car {
            constructor(mark, drive, speed, price, newSpeed) {
                super(mark, drive, speed)
                this.price = price;
                this.newSpeed = newSpeed;
            }
            speedChange() {
                super.show();
                console.log(`${this.mark}价格是${this.price}`);
                console.log(`${this.mark}车现在的速度是${this.newSpeed}`);
            }
        }

        class SubCar2 extends Car {
            constructor(mark, drive, speed, price, newSpeed) {
                super.speedChange(mark, drive, speed)
                this.price = price;
                this.newSpeed = newSpeed;
            }
            show() {
                super.speedChange();
                console.log(`${this.mark}价格是${this.price}`);
                console.log(`${this.mark}车现在的速度是${this.newSpeed}`);
            }
        }
        let car1 = new SubCar1('奥迪', '行驶', 80, 50, 120);
        car1.speedChange();

        console.log('--------------------- ')

        let car2 = new SubCar1('奔驰', '行驶', 100, 90, 120);
        car2.speedChange();

你可能感兴趣的:(前端,javascript,vue.js)