将类类型通过亚型作为参数
如何使用子类型class< foo< bar>>如何创建给定通用类型类的实例。 type =?
?
对于简单的通用,例如:class< foo> type = foo.class;
但是如何在此处执行此操作?
final Class<Foo<Bar>> type = null; // ???
据我所知,我们不能做类似foo&lt; bar&gt; .class
。
我想在一个通用函数中使用此类型,该功能适用于所有简单的仿制药,但不能嵌套。
Foo<Bar> fooBar = getRecord(type, 1);
static T get(Class<T> type, int id)
{
// load from database
return session.get(type, id);
}
复杂的示例
生产示例更复杂,具有许多依赖通用类型的方法的DAO类。
interface Entity {}
class Foo<T extends Entity> extends Entity {}
class Bar extends Entity {}
class EntityDAO<T extends Entity>
{
Class<T> type;
Session session;
public EntityDAO(Class<T> type)
{
this.type = type;
}
public T get(int id)
{
return session.get(type, id);
}
}
public class TestSession
{
public <T> T get(final Class<T> type, final int id)
{
Objects.requireNonNull(type);
return null;
}
}
void static main(String[] args)
{
final Class<Foo<Bar>> type = null;
// how to set type here?
EntityDAO<Foo<Bar>> fooBarDAO = new EntityDAO<>(type);
Foo<Bar> entity = fooBarDAO.get(1);
}
当前,我只是使用Main Type foo.class
失去DAO的bar.class
信息。
How can i create an instance of the given generic type class with a subtype Class<Foo<Bar>> type = ?
?
Its easy for simple generic such as: Class<Foo> type = Foo.class;
but how to do this here?
final Class<Foo<Bar>> type = null; // ???
We cannot do something like Foo<Bar>.class
which is possible in c# as far as i know.
I want to use this type in a generic function which works for all simple generics but not if nested.
Foo<Bar> fooBar = getRecord(type, 1);
static T get(Class<T> type, int id)
{
// load from database
return session.get(type, id);
}
Complex example
The production example is more complex having a DAO class with many methods that rely on the generic type.
interface Entity {}
class Foo<T extends Entity> extends Entity {}
class Bar extends Entity {}
class EntityDAO<T extends Entity>
{
Class<T> type;
Session session;
public EntityDAO(Class<T> type)
{
this.type = type;
}
public T get(int id)
{
return session.get(type, id);
}
}
public class TestSession
{
public <T> T get(final Class<T> type, final int id)
{
Objects.requireNonNull(type);
return null;
}
}
void static main(String[] args)
{
final Class<Foo<Bar>> type = null;
// how to set type here?
EntityDAO<Foo<Bar>> fooBarDAO = new EntityDAO<>(type);
Foo<Bar> entity = fooBarDAO.get(1);
}
Currently i am just using the main type Foo.class
loosing the Bar.class
information for the DAO.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
类别的任何解决方法, type;
作为变量&lt; bar&gt;
在运行时丢失,但我们仍然可以实现entitydao
类,以支持foo&lt bar&gt;
为了更轻松
; @Japhei在他的评论(可与原始代码一起使用)中指出,但并非总是一个虚拟实例,很容易创建或可用。
With any workaround using
Class<Foo<Bar>> type;
as a variable the<Bar>
is lost at runtime but still we can achieve theEntityDAO
class to be as generic to supportFoo<Bar>
for easier code allow type to extendT
inside the constructor / method and cast like:Then use it:
This way we can use it without a dummy instance as @Japhei stated in his comment (which works with the original code) but not always a dummy instance is easy to create or available.
如果您有此类型的实例,则可以将其投入到特定类:
If you have an instance of this type you could cast it to the specific class: