Java泛型通配符及其局限性

rub*_*buc 6 java generics

我有两个问题

我的书中说明了"如果没有上限指定了通配符,那么只能在通配符类型的值上调用Object类型的方法"

我不知道这可能是什么意思.这是什么意思?

对于外卡类型(无界和有界)有什么限制?例如,如果我有一个引用MyClass<?>或者MyClass<? extends SomeOtherClass>,我不允许通过该引用调用哪些方法.我不明白外卡允许或禁止我做什么,这可能是我不理解书中引用的原因.

我有第二部分的例子:

class SomeOtherClass
{
[...]
}

class MyClass<T>
{
[...]
}

class Test
{
     public static void main(String[] arg)
     {
         MyClass<? extends SomeOtherClass> myClass = new MyClass<String>() // for instance what does the wild card reference limit me to in any way. In a general sence.
     }
}
Run Code Online (Sandbox Code Playgroud)

Aur*_*bon 10

对于返回参数化类型对象的集合和类,通配符边界(上部和下部)通常是必需的.

你会经常听到PECS,这意味着"制片人扩展,消费者超级".我建议你阅读这个问题的答案,以避免重复答案.

  • 更准确地说,当您使用时定义通配符时<? extends TheClass>,您告诉编译器通配对象至少是类型TheClass.因此,您可以像使用此实例一样使用此对象TheClass,并调用此类型建议的任何方法.

  • 现在,当您将通配符定义为时<? super TheClass>,您告诉编译器您的通配对象类型是由TheClass类型实现或扩展的.这意味着对象类型可能不是TheClass,但是TheClass对象可以用作通配引用的实例.因此,您无法在该对象上调用任何内容,因为其类型仅在运行时已知,但您可以将该对象传递给等待通配对象的方法.

例子:

private void foo(List<?> list) {
    Object o = list.get(0); // ok
    list.add(new Object()); // won't compile!

    // you cannot add anything, and only extract Object instances
}

private void foo(List<? extends TheClass> list) {
    Object o1 = list.get(0);   // ok
    TheClass o2 = list.get(0); // ok
    list.add(new Object());    // won't compile!
    list.add(new TheClass());  // won't compile!

    // You are sure that the objects are of a subtype of TheClass,
    // so you can extract TheClass instances safely. However, you cannot
    // add anything to this list since its type is not known (may be
    // different from TheClass, so the compiler does not allow anything).
}

private void foo(List<? super TheClass> list) {
    Object o1 = list.get(0);   // ok
    TheClass o2 = list.get(0); // won't compile!
    list.add(new Object());    // won't compile!
    list.add(new TheClass());  // ok

    // You are sure that the objects are of a type implemented by TheClass,
    // so you can add any TheClass instances to the list. However, you cannot
    // extract TheClass objects since the objects type may be just implemented
    // by TheClass, but different.
}
Run Code Online (Sandbox Code Playgroud)