您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

为什么将ArrayList的泛型转换为超类不起作用?

为什么将ArrayList的泛型转换为超类不起作用?

问题是这样的:

ArrayList<A> aList = new ArrayList<A>();
ArrayList<B> bList = new ArrayList<B>();
aList = bList; // if this were valid...
aList.add(new A()); // ...what should happen here?
B b = bList.get(0); // ...and here?

如果对数组执行相同的操作,则在运行时会在第4行获得ArrayStoreException。对于泛型集合,已决定在编译时避免这种情况。

其他 2022/1/1 18:35:14 有229人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶