簡體   English   中英

子類如何獲取超類的實例變量?

[英]How can subclass get superclass' instance variable?

在下面的代碼中,有2個類,一個是Node,另一個是Btree。 如果在Node上調用split()實例,那么我想創建一個新節點,將其另存為父節點,然后更改Btree的根節點。

節點如何訪問Btree.root? 我必須使用類繼承嗎? (此代碼不是完整的代碼,因此可能會有一些錯誤...盡管我只是想了解一下)

    Node = function(dimension,root){
        this.root = root;
        this.parent = null;
    }

    Node.prototype.split = function(
        var tmp = new Node();
        if(!this.parent){
            var soon_to_be_root = new Node();
            this.parent = soon_to_be_root;
        }
    }
    Btree = function(dimension){
        this.d = dimension;
        this.root = new Node(dimension,true);
    }

如果Btree是單例對象,則:

var soon_to_be_root = new Node;
Btree.root = soon_to_be_root;

如果Btree是一個類,並且您有許多實例,則需要將它們關聯。 節點是否“具有” Btree? Btree是否“具有”節點? 如果其中任何一個正確,則在構造另一個實例時,應傳遞一個實例。

另一方面,如果Node“是” Btree或Btree“是” Node,則繼承是適當的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM