簡體   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