繁体   English   中英

使用Google Closure进行多级继承

[英]Multiple level inheritance using google closure

我想使用Google Closure实现多重继承。 我已经研究过,找到了这本书 在第158页,他们说Google闭包不支持多重继承,但是还有其他方法可以做到这一点,例如使用“ goog.mixin”。 我尝试过,但是收到“ Uncaught AssertionError:失败”。

基本上,我想做这样的事情:

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

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

class C extends B {
    function moveBottom() {
        ...
    }
}
  • A类具有方法“ moveLeft”和“ moveRight”。
  • B类具有方法“ moveLeft”,“ moveRight”和“ moveTop”(正常继承,从父类A获取方法)
  • C类应具有方法“ moveLeft”,“ moveRight”,“ moveTop”和“ moveBottom”(双重继承,从父类B和祖父母类A获取方法)

但是,类C仅按照我的方式获取类B的方法。

我该如何使用Google Closure执行此操作?

谢谢。

若昂


编辑1

我会努力使自己更加清晰。 由于专业原因,我无法在此处显示整个代码。 这有点像我的班级样子。

// 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);

目的是构建一个SDK,我在这里将其命名为“ xgis”。 我们想在OpenLayers 3(ol3)之上构建我们的API。 我们希望我们自己的方法使用ol3方法。 我们需要有自己的记录方法。

例如,我想要我自己的方法来检查图层的可见性。 但是此方法必须使用ol3中的“与我的名字相同”的方法:

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

我试图使用关键字“ superClass_”来获取父类的方法,但是没有用。

还有另一种方法吗?

您可以将示例编写为有效的JavaScript吗? 据我所知,这不是“多重继承”,而是多重继承。

我想你的意思是:

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