子类如何获得超类?实例变量?
在下面的代码中,有2个类,一个是Node,另一个是Btree。如果在 Node 上调用 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);
}
In a code below, there are 2 classes, one is Node and the other one is Btree. If split() instance is called on Node, than I would like to create new node, save it as parent, and change the Btree's root node.
How can Node access Btree.root?
Do I have to use class inheritance??
(This code is not complete code so there may be some error...although I just want to get an idea of it)
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果 Btree 是一个单例对象,那么:
如果 Btree 是一个类并且您有许多它们的实例,那么您需要将它们关联起来。节点是否“拥有”B 树? Btree“有”节点吗?如果其中任何一个是正确的,那么您应该在构造另一个实例时传递一个实例。
另一方面,如果一个 Node“是一个”Btree 或一个 Btree“是一个”Node,那么继承是合适的。
If Btree is a singleton object, then:
If Btree is a class and you have many instances of them, then you need to associate them. Does a Node "have a" Btree? Does a Btree "have a" Node? If either of these are correct, then you should pass the instance of one when constructing the instance of the other.
If, on the other hand, a Node "is a" Btree or a Btree "is a" Node, then inheritance is appropriate.