[英]Create list of subclass elements in java generics
i saw that those reflection problems are discussed a lot here. 我看到这些反射问题在这里进行了很多讨论。 Unfotunately i couldnt find any solution for my problem:
不幸的是,我找不到任何解决方案来解决我的问题:
I have one abstract class which handles a lot of stuff for the sublcasses 我有一个抽象类,可以为sublcasses处理很多东西
public abstract class A{
public static <T extends A> create(){
// some factory stuff here
}
public static List<....> createMore(){ /// some stuff here}
}
The class A is located in a included library of the main project. A类位于主项目的随附库中。 Now i want to use the methods of A for the sub classes of A:
现在我想对A的子类使用A的方法:
public class A1 extends A {...}
and use it: 并使用它:
A1 mySubclass = A1.create();
ArrayList<A1> listOfSubclass= A1.createMore();
Is this possible in any way? 这有可能吗?
createMore
is possible: createMore
是可能的:
public static <T extends A> List<T> createMore(){
return new ArrayList<T>();
}
create
is only (correctly) possible if you return null
: 仅当返回
null
才可能(正确) create
:
public static <T extends A> T create(){
return null;
}
Without any input, there is no way that that function can decide what to return. 没有任何输入,该函数将无法决定要返回的内容。 And it must match whatever type the caller scope wants at compile time, without knowing what that is.
而且,它必须与调用方作用域在编译时需要的任何类型相匹配,而不知道那是什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.