如何在Javascript中使用多态

时间:2016-06-28 21:53:11

标签: javascript inheritance polymorphism

大多数语言对类使用单继承。这样做的模式相当明显(例如在下面的Swift代码中)。我仍然试图在JavaScript中围绕模式来创建对象层次结构并重用类函数并覆盖类函数

class animal {
    func talk() {
        print ("?")
    }
}

class bird : animal {
    override func talk() {
        print("tweet tweet")
    }
    func fly() {
        print("flap flap")
    }
}

class parrot : bird {
    override func talk() {
        print("polly want a cracker")
    }
}

var a = animal()
var b = bird()
var p = parrot()

a.talk()  /// ?
b.talk()  /// tweet tweet
b.fly()   /// flap flap
p.talk()  /// polly want a cracker
p.fly()   /// flap flap

我认为我的问题是Javascript代码看起来不像这样。什么是等效的Javascript代码,所以我可以找出模式?

2 个答案:

答案 0 :(得分:7)

你几乎回答了自己的问题。你只需要学习JavaScript的语法。

  

我认为我的问题是Javascript代码看起来不像这样。

  1. 如果某种语言与另一种语言不同,那对您来说不是“问题”

  2. 您提供的Swift代码在语法上非常接近您需要编写以表达相同层次结构的JavaScript(ES6)

  3. class Animal {
      talk() {
        console.log('?')
      }
    }
    
    class Bird extends Animal {
      talk() {
        console.log('tweet tweet')
      }
      fly() {
        console.log('flap flap')
      }
    }
    
    class Parrot extends Bird {
      talk() {
        console.log('polly want a cracker')
      }
    }
    
    var a = new Animal()
    var b = new Bird()
    var p = new Parrot()
    
    a.talk()
    b.talk()
    b.fly()
    p.talk()
    p.fly()

    如果您想在 ES5 中设置“类”继承,则可以执行此操作

    // Animal "class"
    function Animal() {}
    
    // Animal methods
    Animal.prototype.talk = function talk() {
      console.log('?')
    }
    
    // ------------------------------
    // Bird "class"
    function Bird() {
      // if you want to call the parent constructor, you can do that here
      // Animal.call(this, arg1, arg2, ... argN)
    }
    
    // Bird inherits from Animal
    Bird.prototype = Object.create(Animal.prototype)
    Bird.prototype.constructor = Bird
    
    // Bird methods
    Bird.prototype.talk = function() {
      console.log('tweet tweet')
    }
    Bird.prototype.fly = function() {
      console.log('flap flap')
    }
    
    // ------------------------------
    // Parrot "class"
    function Parrot() {
      // if you want to call the parent constructor, you can do that here
      // Bird.call(this, arg1, arg2, ... argN)
    }
    
    // Parrot inherits from Bird
    Parrot.prototype = Object.create(Bird.prototype)
    Parrot.prototype.constructor = Parrot
    
    // Parrot methods
    Parrot.prototype.talk = function() {
      console.log('polly want a cracker')
    }
    
    var a = new Animal()
    var b = new Bird()
    var p = new Parrot()
    
    a.talk()
    b.talk()
    b.fly()
    p.talk()
    p.fly()

答案 1 :(得分:2)

有两个答案,ES6:



class animal {
    talk() {
        console.log("?")
    }
}

class bird extends animal {
    talk() {
        console.log("tweet tweet")
    }
    fly() {
        console.log("flap flap")
    }
}

class parrot extends bird {
    talk() {
        console.log("polly want a cracker")
    }
}

var a = new animal()
var b = new bird()
var p = new parrot()

a.talk()  /// ?
b.talk()  /// tweet tweet
b.fly()   /// flap flap
p.talk()  /// polly want a cracker
p.fly()   /// flap flap




和ES5:



function animal() {

}

animal.prototype.talk = function () {
    console.log("?")
};

function bird() {
    animal.call(this)
}

bird.prototype = Object.create(
    animal.prototype,
    {constructor: {value: bird}}
);

bird.prototype.talk = function () {
    console.log("tweet tweet")
};

bird.prototype.fly = function () {
    console.log("flap flap")
};

function parrot() {
    bird.call(this);
}

parrot.prototype = Object.create(
    bird.prototype,
    {constructor: {value: parrot}}
);

parrot.prototype.talk = function () {
    console.log("polly want a cracker")
};


var a = new animal()
var b = new bird()
var p = new parrot()

a.talk()  /// ?
b.talk()  /// tweet tweet
b.fly()   /// flap flap
p.talk()  /// polly want a cracker
p.fly()   /// flap flap