简体   繁体   English

使用Google Closure进行多级继承

[英]Multiple level inheritance using google closure

I want to implement multiple inheritance using google closure . 我想使用Google Closure实现多重继承。 I have already researched and I found this book . 我已经研究过,找到了这本书 At page 158, they say that google closure doesn't support multiple inheritance, but that there are other ways to do it, like using "goog.mixin". 在第158页,他们说Google闭包不支持多重继承,但是还有其他方法可以做到这一点,例如使用“ goog.mixin”。 I tried it but I get "Uncaught AssertionError: Failure". 我尝试过,但是收到“ Uncaught AssertionError:失败”。

Basically, I want to do something like this: 基本上,我想做这样的事情:

class A {
    function moveLeft() {
        ...
    },
    function moveRight() {
        ...
    }
}

class B extends A {
    function moveTop() {
        ...
    }
}

class C extends B {
    function moveBottom() {
        ...
    }
}
  • Class A has the methods "moveLeft" and "moveRight". A类具有方法“ moveLeft”和“ moveRight”。
  • Class B has the methods "moveLeft", "moveRight" AND "moveTop" (normal inheritance, gets the methods from the parent class A) B类具有方法“ moveLeft”,“ moveRight”和“ moveTop”(正常继承,从父类A获取方法)
  • Class C should have the methods "moveLeft", "moveRight", "moveTop" AND "moveBottom" (double inheritance, gets the methods from the parent class B, and the grandparent class A) C类应具有方法“ moveLeft”,“ moveRight”,“ moveTop”和“ moveBottom”(双重继承,从父类B和祖父母类A获取方法)

BUT class C only gets the methods of class B the way I am doing. 但是,类C仅按照我的方式获取类B的方法。

How could I do this using google closure? 我该如何使用Google Closure执行此操作?

Thank you. 谢谢。

João 若昂


EDIT 1 编辑1

I will try to make myself clearer. 我会努力使自己更加清晰。 I can't display here the whole code for professional reasons. 由于专业原因,我无法在此处显示整个代码。 This is a bit how my classes look like. 这有点像我的班级样子。

// external file xgis.js

xgis = {};


// xgis.map
xgis.map = function(options) {
    // map definitions ...
};
// Inherits from ol.Map
goog.inherits(xgis.map, ol.Map);


// xgis.layer
xgis.layer = function(options) {
    // base layer definitions
};


// xgis.layer.osm
xgis.layer.osm = function(options) {
    goog.base(this, {
        source: new ol.source.OSM()
    });
    sigga.layer.call(this, options);
};
// Inherits from ol.layer.Tile
goog.inherits(xgis.layer.osm, ol.layer.Tile);
/**
 * Copies all the members of a source object to a target object.
 * i.e, inherits ALSO from xgis.layer (the base layer class)
 */
goog.mixin(xgis.layer.osm.prototype, xgis.layer.prototype);

The goal is to build an SDK, which I named here "xgis". 目的是构建一个SDK,我在这里将其命名为“ xgis”。 We want to build our API on top of OpenLayers 3 (ol3). 我们想在OpenLayers 3(ol3)之上构建我们的API。 We want our own methods to use the ol3 methods. 我们希望我们自己的方法使用ol3方法。 We need to have our own method for documenting. 我们需要有自己的记录方法。

For example, I want my own method to check the visibility of a layer. 例如,我想要我自己的方法来检查图层的可见性。 But this method has to use the one from ol3 "with the same name as mine": 但是此方法必须使用ol3中的“与我的名字相同”的方法:

// My method
xgis.layer.prototype.getVisible = function() {
    // Used the method of the parent class from ol3
    return this.superClass_.getVisible();
};

I tried to use the keyword "superClass_" to get the method of the parent class, but it didn't work. 我试图使用关键字“ superClass_”来获取父类的方法,但是没有用。

Is there another way? 还有另一种方法吗?

Can you write your example as valid JavaScript? 您可以将示例编写为有效的JavaScript吗? As far as I can tell this isn't "multiple inheritance" but simply multiple levels of inheritance. 据我所知,这不是“多重继承”,而是多重继承。

I think you mean: 我想你的意思是:

function A() {}
A.prototype.moveLeft = function() {};

function B() {}
goog.inherits(B, A);
A.prototype.moveTop = function() {};

function C() {}
goog.inherits(C, B);
A.prototype.moveBottom = function() {};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM