B 树实现 - 我是否应该将 Node 类设为静态成员类?
我需要为大学实现一棵 B 树:
我有一个“外部”类 B 树,其属性为 root 和 _ Degree。表示节点的类被实现为静态成员类:
public class BTree<E> {
private Node<E> root;
// the minimal degree
private int degree;
public BTree(int degree) {
if (degree < 2) {
throw new IllegalArgumentException();
}
this.degree = degree;
}
// other stuff
private static class Node<T> {
T[] elements = (T[])new Object[degree * 2 - 1];
Node<T>[] children = (Node<T>[])new Object[degree * 2];
int size = 0;
}
}
所以,现在我的问题是: 当我将 Node 类实现为静态成员类时,我无法访问外部类的 Degree 属性。
现在我必须选择:
- 使 Node 类成为内部类(非静态成员类)或
- 为 Node 类创建一个构造函数,并在每次需要构造 Node 时传递度数。
最好的选择是什么?使其成为内部类意味着节点都将引用 Btree(外部类),但使其成为静态成员类意味着我每次都必须传递学位。
I need to implement a B-Tree for University:
I have an "outer" class B-Tree with attributes root and _degree. The class to represent the nodes is implemented as a static-member class:
public class BTree<E> {
private Node<E> root;
// the minimal degree
private int degree;
public BTree(int degree) {
if (degree < 2) {
throw new IllegalArgumentException();
}
this.degree = degree;
}
// other stuff
private static class Node<T> {
T[] elements = (T[])new Object[degree * 2 - 1];
Node<T>[] children = (Node<T>[])new Object[degree * 2];
int size = 0;
}
}
So, now my problem is: As I implemented the Node class as a static member class, I can't access the degree attribute of the outer class.
Now I have to choices:
- Make the Node class an inner class (non-static member class) OR
- Create a constructor for the Node class and pass the degree in every time I need to construct a Node.
What would be the best choice? Making it an inner class would mean the Nodes would all have a reference to the Btree (outer class), but making it a static member class would mean I would have to pass the degree in every time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会将其保持静态并传递学位。这样您就可以确保
Node
无法了解有关BTree
的任何详细信息。I would keep it static and pass degree in. That way you ensure that
Node
cannot know any details aboutBTree
.如果是我,我会将
Node
类公开,这样我就可以在其他包含数据结构中重用它,但这只是我。在这种情况下,我必须通过构造函数传递学位,这对我来说也没关系。我不喜欢内部类操纵封闭类的成员的想法。我觉得这使得各个班级之间的联系过于紧密。我知道有时这是合适的,但我会尽可能避免,这似乎是一个很容易避免的情况。If it were me, I'd have the
Node
class public so I could reuse it in other containing data structures, but that's just me. In that case, I'd have to pass the degree through the constructor, and that's OK with me too. I don't like the idea of inner classes manipulating the members of enclosing classes. I feel it makes the classes too tightly bound to each other. I know sometimes it's appropriate, but I avoid when I can and this seems an easily avoidable case.有观点认为它是静态的,因为它解耦了类。
但我认为 BTree.Node 是特定 BTree 的节点。创建一堆节点(具有随机度数)是没有意义的。你不能有节点,但没有树。因此,我说非静态。
There are arguments to make it static, because it decouples the classes.
But I think a BTree.Node is a node from a particular BTree. It doesn't make sense to go and create a bunch of Nodes (with random degrees). You can't have a Node, but no Tree. Thus, I say non-static.