gpt4 book ai didi

java - 了解上界和下界?在 Java 泛型中

转载 作者:IT老高 更新时间:2023-10-28 21:08:58 25 4
gpt4 key购买 nike

我真的很难理解通配符参数。我对此有几个问题。

  1. ?作为类型参数只能在方法中使用。例如:printAll(MyList<? extends Serializable>)我不能用 ? 定义类作为类型参数。

  2. 我了解 ? 的上限. printAll(MyList<? extends Serializable>)表示:“ printAll 将打印 MyList 如果它有实现 Serialzable 接口(interface)的对象。
    我对 super 有点问题. printAll(MyList<? super MyClass>)表示:“ printAll 将打印 MyList 如果它有 MyClass 或任何扩展 MyClass 的类(MyClass 的后代)的对象。

纠正我哪里出错了。

简而言之,只有 TEKVN可以用作定义泛型类的类型参数。 ?只能在方法中使用


更新 1:

public void printAll(MyList<? super MyClass>){
// code code code
}

根据 Ivor Horton 的书,MyList<? super MyClass>表示我可以打印MyList如果它有 MyClass 的对象或它实现的任何接口(interface)或类。也就是说,MyClass是一个下限。它是继承层次结构中的最后一个类。这意味着我最初的假设是错误的。

所以,如果 MyClass看起来像:

public class MyClass extends Thread implements ActionListener{
// whatever
}

那么,printAll()将打印如果
1.有MyClass的对象在列表中
2.有Thread的对象或 ActionListenerList


更新 2:

所以,在阅读了这个问题的许多答案之后,这是我的理解:

  1. ? extends T表示任何扩展 T 的类。因此,我们指的是T子代。 。因此, T是上限。继承层次结构中最上层的类

  2. ? super T表示任何类/接口(interface)superT 。因此,我们指的是T 的所有 parent 。 T因此是下界。继承层次结构中最底层的类

最佳答案

? as a type parameter can only be used in methods. eg: printAll(MyList<? extends Serializable>) I cannot define classes with ? as type parameter.

通配符 (?) 不是正式的类型参数,而是可以用作类型参数。在您给出的示例中, ? extends Serializable 作为 MyList 方法参数的泛型类型 printAll 的类型参数给出。

方法 can also declare type parameters 之类的类,例如:

static <T extends Serializable> void printAll(MyList<T> myList)

I understand the upper bound on ?. printAll(MyList<? extends Serializable>) means printAll will print MyList if it has objects that implement the Serialzable interface

更准确地说,这意味着printAll 的调用只有在传递一个具有某种泛型类型的 MyList 或实现 Serializable 时才会编译。在这种情况下,它将接受 MyList<Serializable>MyList<Integer> 等。

I have a bit of an issue with the super. printAll(MyList<? super MyClass>) means printAll will print MyList if it has objects of MyClass or any class which extends MyClass (the descendants of MyClass)

super 为界的通配符是 下限。所以我们可以说printAll 的调用只有在传递一个 MyList 的泛型类型为 MyClass 或某些父类(super class)型为 MyClass 时才会编译。所以在这种情况下,它会接受 MyList<MyClass> ,例如MyList<MyParentClass>MyList<Object> .

So, say if MyClass looks like:

public class MyClass extends Thread implements ActionListener{
// whatever
}

then, printAll() will print if

  1. There are objects of MyClass in the list
  2. There are objects of Thread or ActionListener in the list

你在正确的轨道上。但我认为说例如“如果列表中有 MyClass 的对象,它将打印”是有问题的。这听起来像是在定义运行时行为——泛型都是关于编译时检查的。例如,不能通过继承将 MyList<MySubclass> 作为 MyList<? super MyClass> 的参数传递,即使它可能包含 MyClass 的实例。我将其改写为:

printAll(MyList<? super MyClass>) 的调用只有在传递 a: 时才会编译:

  1. MyList<MyClass>
  2. MyList<Thread>
  3. MyList<Runnable>
  4. MyList<ActionListener>
  5. MyList<EventListener>
  6. MyList<Object>
  7. MyList<? super X> 其中 XMyClassThreadRunnableActionListenerEventListenerObject

So, after having read the many answers to the question, here is my understanding:

? extends T means any class which extends T. Thus, we are referring to the children of T. Hence, T is the upper bound. The upper-most class in the inheritance hierarchy

? super T means any class / interface which is super of T. Thus we are referring to all the parents of T. T is thus the lower bound. The lower-most class in the inheritance hierarchy

关闭,但我不会说“T 的 child ”或“T 的 parent ”,因为这些界限包含 - 说“T 或其子类型”会更准确,和“T 或其父类(super class)型”。

关于java - 了解上界和下界?在 Java 泛型中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19795709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com