1

在下面的代码中,有 2 个类,一个是 Node,另一个是 Btree。如果在节点上调用 split() 实例,那么我想创建新节点,将其保存为父节点,然后更改 Btree 的根节点。

Node如何访问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);
    }
4

1 回答 1

0

如果 Btree 是一个单例对象,那么:

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

如果 Btree 是一个类并且你有很多实例,那么你需要关联它们。节点是否“有” Btree?Btree“有”节点吗?如果其中任何一个是正确的,那么您应该在构造另一个实例时传递一个实例。

另一方面,如果节点“是”Btree 或Btree“是”节点,那么继承是合适的。

于 2011-04-18T02:10:52.210 回答