[英]Static methods for operations on inner class
I'm working through Algorithms Fourth Edition (Sedgewick) and am confused by some of the linked-list exercises which seem to be asking to implement static methods for the non-static nodes. 我正在通过算法第四版 (Sedgewick),并且对一些似乎要求为非静态节点实现静态方法的链表练习感到困惑。 For example,
例如,
1.3.27 Write a method
max()
that takes a reference to the first node in a linked list as argument and returns the value of the maximum key in the list.1.3.27编写一个方法
max()
,该方法将链接列表中的第一个节点作为参数引用,并返回列表中最大键的值。 Assume that all keys are positive integers, and return0
if the list is empty.假设所有键都是正整数,如果列表为空则返回
0
。
or 要么
1.3.31 Implement a nested class DoubleNode for building doubly-linked lists, where each node contains a reference to the item preceding it and the item following it in the list (
null
if there is no such item).1.3.31实现嵌套类DoubleNode,用于构建双向链表,其中每个节点包含对其前面的项及其后面的项的引用(如果没有这样的项,则为
null
)。 Then implement static methods for the following tasks: insert at the beginning, insert at the end, remove from the beginning, remove from the end, insert before a given node, insert after a given node, and remove a given node.然后为以下任务实现静态方法:在开头插入,在结尾插入,从开头删除,从末尾删除,在给定节点之前插入,在给定节点之后插入,以及删除给定节点。
As I understand (and confirmed by SO answers here and here ) this isn't possible. 据我了解(并在此处和此处通过SO答案确认),这是不可能的。 As expected, Eclipse gives errors if I try to write a static method in the superclass:
正如所料,如果我尝试在超类中编写静态方法,Eclipse会出错:
public class DoublyLinkedList<Item> {
public static void insertStart(DoubleNode first) {
// implementation
}
private class DoubleNode {
Item item;
DoubleNode next;
DoubleNode previous;
}
}
(gives the error Cannot make a static reference to the non-static type DoubleNode
); (给出错误
Cannot make a static reference to the non-static type DoubleNode
); or in the inner class: 或者在内部阶级:
public class DoublyLinkedList<Item> {
private class DoubleNode {
Item item;
DoubleNode next;
DoubleNode previous;
public static void insertStart(DoubleNode first) {
// implementation
}
}
}
(gives the error The method insertStart cannot be declared static; static methods can only be declared in a static or top level type
). (给出错误
The method insertStart cannot be declared static; static methods can only be declared in a static or top level type
)。
I could write the required methods as instance methods for the DoublyLinkedList
class, and this would seem most natural to me. 我可以将所需的方法编写为
DoublyLinkedList
类的实例方法 ,这对我来说似乎是最自然的。
However, I feel that I might be missing out on something important here. 但是,我觉得我可能会错过这里重要的事情。 The author explicitly states that the methods should be static, and also suggests taking a reference to the first node as an argument (which would be unnecessary for an instance method as the class will have an instance variable for the first node).
作者明确指出方法应该是静态的,并且还建议将第一个节点作为参数引用(这对于实例方法是不必要的,因为该类将具有第一个节点的实例变量)。 What am I missing?
我错过了什么?
You can make nested classes as static
. 您可以将嵌套类设置为
static
。 You will lose the constraint of having an enclosing parent class instance, but it will allow you to operate on DoubleNode
s from a static method : 您将失去具有封闭父类实例的约束,但它将允许您从静态方法操作
DoubleNode
:
// This will compile
public class DoublyLinkedList<Item> {
public static <T> void insertStart(DoublyLinkedList<T> list, DoubleNode<T> first) {
// implementation
}
private static class DoubleNode<E> {
E item;
DoubleNode<E> next;
DoubleNode<E> previous;
}
}
Two things to notice here : As you can see, when making the inner class static, you need to provide it its own type parameter (in this case, E
). 这里要注意两件事:正如您所看到的,当使内部类静态时,您需要为其提供自己的类型参数(在本例中为
E
)。 In your code you didn't need to do that, because any DoubleNode
instance was guaranteed to have a containing DoublyLinkedList
instance, which already determines what Item
will be. 在您的代码中,您不需要这样做,因为任何
DoubleNode
实例都保证包含DoublyLinkedList
实例,该实例已经确定了Item
将是什么。
Secondly, you need to introduce a type parameter for your static method (" <T>
"), so you can enforce the same generic type for both arguments. 其次,您需要为静态方法(“
<T>
”)引入一个类型参数,因此您可以为两个参数强制执行相同的泛型类型。 You could also have done this and get away with it : 你也可以这样做并侥幸逃脱:
public static void insertStart(DoublyLinkedList<?> list, DoubleNode<?> first) {
...
}
In case you're wondering, this is also how it is done in the JDK's LinkedList
implementation : 如果您想知道,这也是在JDK的
LinkedList
实现中完成的方式:
// Source : Oracle JDK 1.7.0_67 lib - inside LinkedList class
private static class Node<E> {
E item;
Node<E> next;
Node<E> prev;
Node(Node<E> prev, E element, Node<E> next) {
this.item = element;
this.next = next;
this.prev = prev;
}
}
As a sidenote, I do agree that it is more natural to write those methods as instance members ; 作为旁注,我同意将这些方法作为实例成员编写更为自然; that's how it's generally done in OOP libraries.
这就是在OOP库中通常完成的。 I don't have a copy of Sedgewick's book but it looks like he's trying to teach you both nested class manipulation and list implementations at the same time ;)
我没有Sedgewick的书的副本,但看起来他正试图同时教你嵌套类操作和列表实现;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.