Fra*_*fka 7 java types casting interface jaxb
我有一些JAXB生成的bean,它们是分层结构,例如一个bean拥有其他bean的列表.现在我想扩展一些子元素以及包含扩展子元素的父元素.
我ParentEx实现了一些其他IParent预期返回的接口Collection<IChild>.我的ChildEx工具IChild.退货(Collection<IChild>)super.getChild()时可以super.getChild()退货List<Child>吗?或者有更好的方法吗?
Child并且Parent是JAXB生成的beanChildEx并且ParentEx是我自己的bean,用于将JAXB bean映射到给定的接口.两个bean都覆盖了ObjectFactoryIChild并且IParent是其他一些库所需的接口编辑: Eclipse甚至不让我的演员来List<Child>,List<ChildEx>所以我必须添加一些丑陋的中间通配符强制转换(List<ChildEx>)(List<?>)super.getChild()
这应该有效:
return new ArrayList<IChild>( childExList );
Run Code Online (Sandbox Code Playgroud)
或(不漂亮但避免通配符转换):
return Arrays.asList( childExList.toArray(new IChild[]{}) );
Run Code Online (Sandbox Code Playgroud)