Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 下限通配符_Java_Generics_Bounded Wildcard - Fatal编程技术网

Java 下限通配符

Java 下限通配符,java,generics,bounded-wildcard,Java,Generics,Bounded Wildcard,我正在努力理解下面的代码片段,在stackoverflow上搜索到与和相关的链接 只是想克服下面几行中的困惑 si=s//好的,为什么?对象数组可以隐式转换为对象数组 整数 List记住,gneric是为了编译器的利益,而不是为了运行时信息 当您执行si=s操作时,您正在将一个数字超类的列表分配给一个变量,该变量可以引用一个整数超类的列表 如果它是一个数字列表,那么它仍然是一个整数超类的列表 我们知道它实际上是一个对象的Arraylist,因为您之前已经指定了它 我们还知道对象的ArrayLis

我正在努力理解下面的代码片段,在stackoverflow上搜索到与和相关的链接

只是想克服下面几行中的困惑

si=s//好的,为什么?对象数组可以隐式转换为对象数组 整数


List记住,gneric是为了编译器的利益,而不是为了运行时信息

当您执行
si=s
操作时,您正在将一个数字超类的列表分配给一个变量,该变量可以引用一个整数超类的列表

如果它是一个数字列表,那么它仍然是一个整数超类的列表

我们知道它实际上是一个对象的Arraylist,因为您之前已经指定了它

我们还知道对象的ArrayList扩展了对象的列表

对象列表是某事物的列表,它是数字的超类,这就是上一个赋值工作的原因


我们也知道对象列表是一个整数超类的列表,但这与编译器无关。它只关心任何数字超类也是整数超类。

我想知道你是否清楚为什么要使用
来正确解析编译器w.r.t泛型,如果我说的话{si数组of super of integer}={s数组of super of number}编译器不关心右侧(super of numbers)的类型参数是否可以转换为左侧(super of integer)..它只关心左类型参数是否为右类型参数的后代/子类型,或者它只关心右类型参数是否为左类型参数的超类型/祖先?不,编译器不关心,但任何数字的超数都是整数的超数,因为数字是整数的超数。另外,请注意将的数组与as数组的列表相等是一些有趣的对象,它们有点像原语,有点像类,整数数组可以分配给数字数组,但整数列表不能分配给数字列表,因为通用系统不允许。我知道数字是整数的超类。假设这种情况,ListFirst我想你不需要键入推断变量是通配符。其次,不要将列表的通用通配符与列表可以包含的项目混淆。区别在于,通配符限制了可以分配的列表类型。列表本身将限制可以添加的内容。可怕的蝌蚪回答在某种程度上解释了这里的问题。以及缺少你在现实世界场景中提供的示例代码中的真实性。谢谢!但是你只能将列表中的成员读入Object类型的变量,除非你使用类型转换。如果你说的是super(下限),我认为这不是真的,将列表的成员读入变量时,可以将其指定给Object(至少),但不能将其指定给Number(或特定的下限类型参数)。Object obj=s.get(0);//工作正常简而言之,情况并非如此,对于下限通配符类型,您可以读取项目,但不能将成员放入结构中。可以读取而不能添加的应为上限谢谢。根据响应进行编辑。
 List<? extends Number> l = new ArrayList<>();
 List<? extends Integer> i = new  ArrayList<>();
 l = i;//OK i is a subtype of l

 List<? super Number> s = new ArrayList<>();
 List<? super Integer> si = new  ArrayList<>();

 si = new ArrayList<Integer>();//OK understand integer matches the pattern of ? super Integer
 s = new ArrayList<Object>();//OK understand that object is superclass of Number

 si=s//OK why?  arrays of objects can be implicitly casted to arrays of integers?

 //consider this
 List<Integer> integers = new ArrayList<Integer>();
 List<Object> objects = new ArrayList<Object>();
 integers = objects; //NOT OK Type mismatch: cannot convert from List<Object> to List<Integer>

 //consider this
 Integer ten = 10; //integer ten
 Object none = new Object();//some none
 ten = none;//NOT OK  none cannot be implicitly casted to ten  
void appendInteger (List <? extends Number> list) {
   list.add (new Integer (3)); // Compiler error on this line
}
void getInteger (java.util.List <? extends Number> list) {
    Number n = list.get (0); // Compiler error on this line
}
Object v = list.get (0);  // this will work
Number n = (Number) list.get (0);  // this will also work