任何人都可以发布此VB.NET代码的Java版本吗?Java中的泛型工厂
Public Function FetchDoc(Of T As {New, IRepoDocument})(ByVal docId As String) As IRepoDocument Implements IDocRepository.FetchDoc
Dim repoDoc As New T
//some code to init repoDoc
Return repoDoc
End Function
这个函数接受和创造实现IRepoDocument
任何类的实例,并且有一个无参数的构造函数。
我发现的唯一方法是:
public <T extends IRepoDocument> IRepoDocument FetchDoc(String idDoc, Class<T> clazz)
throws InstantiationException, IllegalAccessException
{
return clazz.newInstance();
}
但我想抑制Class<T> clazz
作为输入参数。
所以你想要在ClassLoader中搜索任何实现IRepoDocument并实例化它的类? – 2012-04-17 12:39:08
你想如何确定你真正想要的课程? – 2012-04-17 12:41:59
如果没有Java中的Class参数,没有办法做到这一点。 – 2012-04-17 12:42:24