类型不匹配错误的原因:转换的ArrayList< SubClass1>列出<&父类GT ;? [英] Reason of Type mismatch Error : convert ArrayList<SubClass1> to List<SuperClass>?

查看:189
本文介绍了类型不匹配错误的原因:转换的ArrayList< SubClass1>列出<&父类GT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

B类扩展类A.We可以用这个语句:

Class B extends class A.We can use this statement:

A a = new B();

但为什么我们不能用这个code:

But why we can not use this code:

List<A> listOfA = new ArrayList<B>();

当然,我们可以使用仿制药,以避免错误。但我的问题是在大约上升的原因是错误的

Of course,we can use generics to avoid that error.But my question is in about reason of rising that error.

推荐答案

有关我的原因是因为Java开发人员不希望重复他们的错误,他们在阵列一样。例如,

For me the reason would be because the java developer doesn't want to repeat their mistake they did in array. For example,

 Object x[] = new String[3];
 x[0] = new Integer(0);

这code给出了一个运行时异常 java.lang.ArrayStoreException

this code gives a runtime exception java.lang.ArrayStoreException

来源

这篇关于类型不匹配错误的原因:转换的ArrayList&LT; SubClass1&GT;列出&LT;&父类GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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