axuanup 发表于 2024-2-8 00:05:55

aardio实现封装继承多态

本帖最后由 axuanup 于 2024-2-8 00:19 编辑

import console;

//封装
class Animal{
    ctor(name) {
      this.name = name
    }

    eat = function() {
      ..console.log(this.name + " is eating.")
    }
}

//继承
class Carnivore{
    ctor(name) {
      this = ..Animal(name)
    }

    eat = function() {
      ..console.log(this.name + " is eating meat.")
    }
}

//多态
class Herbivore{
    ctor(name) {
      this = ..Animal(name)
    }

    eat = function() {
      ..console.log(this.name + " is eating plants.")
    }
}

var carnivore = Carnivore("Tiger")
var herbivore = Herbivore("Deer")
carnivore.eat()// 输出 "Tiger is eating meat."
herbivore.eat()// 输出 "Deer is eating plants."

console.pause(true);

AdGame 发表于 2024-2-12 03:59:08

支持了,学习下
页: [1]
查看完整版本: aardio实现封装继承多态