简体   繁体   English

Java-为什么在这种情况下静态很重要

[英]Java - Why static is important in this case

I've two questions that relates to the following code (Linked Bag): 我有两个与以下代码(链接包)相关的问题:

public class LinkedBag<Item> implements Iterable<Item> {

private Node first;    // beginning of bag
private int N;         // number of elements in bag

private class Node {
    private Item item;
    private Node next;
}

and this one 还有这个

public class Bag<Item> implements Iterable<Item> {
private Node<Item> first;    // beginning of bag
private int N;               // number of elements in bag

// helper linked list class
private static class Node<Item> {
    private Item item;
    private Node<Item> next;
}
  1. What's the difference between Node<Item> and Node here? 这里的Node<Item>和Node有什么区别? both implementations are generic, so how Node and Node<Item> differ here ? 两种实现都是通用的,所以Node和Node<Item>在这里有何不同?
  2. What's the meaning of static in the second version, why is it crucial ? 第二版中static的含义是什么,为什么至关重要? ( doesn't compile without static). (在没有静态的情况下不会编译)。

Thanks ! 谢谢 !

If your nested class is not static , then it's an inner class, and the enclosing class's type parameter Item is in scope. 如果您的嵌套类不是static ,则它是一个内部类,并且封闭类的类型参数Item在范围内。 However, if your nested class is static , then the enclosing class's type parameter is not in scope, because that type parameter has to do with an instance of the enclosing class, which is not relevant for a static nested class. 但是,如果嵌套类是static ,则封闭类的type参数不在范围内,因为该类型参数与封闭类的实例有关,而该实例与static嵌套类无关。

Here, the static class Node declares its own Item type parameter. 在这里, staticNode声明了自己的Item类型参数。 It could have declared any other name and it would have been equivalent. 它可以声明任何其他名称,并且将是等效的。 You should not receive a compiler error using Node<Item> in your second case (with the static class); 在第二种情况下(使用static类),不应使用Node<Item>收到编译器错误。 that is legal. 那是合法的。

  1. What's the difference between Node<Item> and Node here? 这里的Node<Item>Node什么区别?

Adding a generic parameter is necessary because Java prohibits static inner classes of generic types from referencing type parameters of their outer types. 添加泛型参数是必要的,因为Java禁止泛型的static内部类引用其外部类型的类型参数。

  1. What's the meaning of static in the second version? 第二版中static的含义是什么?

static means that Node instances do not get a reference to their parent class, and can be created outside of its context if it is necessary. static表示Node实例没有获得对其父类的引用,并且可以在必要时在其上下文之外创建。 It is not exactly "crucial", but shrinks your node by a third (two references vs. three references), which is an advantage. 它不是完全“关键的”,而是将您的节点缩小三分之一(两个引用与三个引用),这是一个优势。

If a nested class is not static (called an inner class ), it means that every instance belongs to an instance of the enclosing class. 如果嵌套类不是静态的(称为内部类 ),则意味着每个实例都属于封闭类的实例。 Therefore in the first example, a Node instance belongs to a LinkedBag<Item> , so it already has a generic type Item (from LinkedBag<Item> ). 因此,在第一个示例中, Node实例属于LinkedBag<Item> ,因此它已经具有通用类型Item (来自LinkedBag<Item> )。

An instance of a static nested class does not belong to an instance of the enclosing type, so it does not get the generic type parameter from an enclosing instance - you need to give it its own generic parameter. 静态嵌套类的实例属于封闭类型的实例,因此它不会从封闭实例中获取泛型类型参数-您需要为其提供自己的泛型参数。

Looking at the source code for the various Collection and Map implementations in the standard Java collections framework, you can see that both approaches to Node classes (static and non-static) are commonly used. 查看标准Java集合框架中各种CollectionMap实现的源代码,您会发现两种使用Node类的方法(静态和非静态)都是常用的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM