列表与LT;?> vs List&lt ;?扩展对象> [英] List<?> vs List<? extends Object>

查看:137
本文介绍了列表与LT;?> vs List&lt ;?扩展对象>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:

<?>有什么区别?和<?扩展对象>在Java泛型?


我发现 List< ?> 列表< ;?扩展对象> 以相同的方式操作。至于我,他们之间没有区别。如果我不对,你能解释一下我的区别吗?

  import java.util.ArrayList; 
import java.util.List;

public class TestClass {

static void func1(List<?> o,Object s){
o.add(null); //只有空
o.add(s); //错误
o.get(0); //确定
}

static void func2(List< ;? extends Object> o,Object s){
o.add(null); //只有空
o.add(s); //错误
o.get(0); //新增了ArrayList< String>(),new Integer(1)); //确定
}

public static void main(String [] args){
func1
func2(new ArrayList< String>(),new Integer(1));

列表< ;?扩展对象> list1 = new ArrayList< Object>();
列表<?> list2 = new ArrayList< Object>();

列表< ;?扩展对象> list3 = new ArrayList< String>();
列表<?> list4 = new ArrayList< String>();
}
}


解决方案

它对于任何类型的变量 T ,规范说 /docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.4\">http://docs.oracle.com/javase/specs/jls/se7/html/jls- 4.html#jls-4.4


每个类型变量都有一个绑定。如果没有为类型变量声明绑定,则假定为Object。


有人会认为通配符也是如此,应该只是的缩写?扩展对象



然而,通过规范搜索,没有证据表明通配符必须具有上限(或下限) 。 无界的与有界的通配符一致。



我们可以从子类型规则中推断出 List<?> List< ;? extends Object> 是彼此的子类型,即它们基本上是相同的类型。 (扣除取决于接口List 中的 E 具有隐含的上限 Object ;但规则不需要通配符的限制)



尽管如此,该规范对待两者的方式不同。例如 http://docs.oracle.com/javase/ specs / jls / se7 / html / jls-4.html#jls-4.7 List<?> 是一个可定义类型,但列表与LT ;?扩展对象> 不是,这意味着

  // ok 
列表<?> ; [] xx = {};
//失败
列表<? extends Object> [] yy = {};

// ok
boolean b1 =(y instanceof List<?>);
// fail
boolean b2 =(y instanceof List< ;? extends Object>);

我不明白为什么。说一个通配符必须有一个上限和一个下限,默认为 Object null type

Possible Duplicate:
What’s the difference between <?> and <? extends Object> in Java Generics?

I found that List<?>and List<? extends Object> act in the same way. As for me, there are no difference between them. If I am not right, can you explain me the difference?

import java.util.ArrayList;
import java.util.List;

public class TestClass {

static void func1(List<?> o, Object s) {
    o.add(null); // only null
    o.add(s); // wrong
    o.get(0);  // OK
}

static void func2(List<? extends Object> o, Object s) {
    o.add(null); // only null
    o.add(s); // wrong
    o.get(0); // OK
}

public static void main(String[] args) {
    func1(new ArrayList<String>(), new Integer(1));
    func2(new ArrayList<String>(), new Integer(1));

    List<? extends Object> list1 = new ArrayList<Object>();
    List<?> list2 = new ArrayList<Object>();

    List<? extends Object> list3 = new ArrayList<String>();
    List<?> list4 = new ArrayList<String>();
}
}

解决方案

It is complicated...

For any type variable T, the spec says http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.4

Every type variable ... has a bound. If no bound is declared for a type variable, Object is assumed.

One would think that it's true for wildcard too, and ? should just be a shorthand for ? extends Object.

Yet searching through the spec, there is no evidence at all that a wildcard must have an upper bound (or lower bound). The "unbounded" ? is treated consistently distinctly from bounded wildcards.

We could deduce from subtyping rules, that List<?> and List<? extends Object> are subtypes of each other, i.e., they are basically the same type. (The deduction depends on the fact that E in interface List<E> has an implicit upper bound Object; but the rules do not require bounds on wildcards)

Nevertheless the spec treats the two differently. For example http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.7 List<?> is a reifiable type, but List<? extends Object> is not, which means

    // ok
    List<?>[] xx = {};
    // fail
    List<? extends Object>[] yy = {};

    // ok
    boolean b1 = (y instanceof List<?>);
    // fail
    boolean b2 = (y instanceof List<? extends Object>);

I don't understand why though. It seems perfectly fine to say a wildcard must have an upper bound and a lower bound, default to Object and null type.

这篇关于列表与LT;?&GT; vs List&lt ;?扩展对象&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆