今天我遇到了一个有趣的问题。考虑下面的代码无法投射通用集?
public static class Parent {}
public static class Child extends Parent {}
Set<Child> childs = new HashSet();
Set<Parent> parents = (Set<Parent>)childs; //Error: inconvertible types
Parent parent = (Parent)new Child(); //works?!
为什么不会像这样的工作?由于泛型的各种规则,我希望隐式转换不起作用,但为什么不能进行显式转换?
'可能是。 – 2011-06-11 23:46:18
@ahmet说'HashSet'_where?_ –
2011-06-12 00:01:12