将类类型通过亚型作为参数

发布于 2025-02-13 23:18:41 字数 1491 浏览 2 评论 0原文

如何使用子类型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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

债姬 2025-02-20 23:18:41

使用类别的任何解决方法, type;作为变量&lt; bar&gt;在运行时丢失,但我们仍然可以实现entitydao类,以支持foo&lt bar&gt;为了更

@SuppressWarnings("unchecked")
public <V extends T> TestDAO(final Class<V> type)
{
    this.type = (Class<T>) type;
}

轻松

void static main(String[] args)
{
    // <Bar> information lost at runtime but not important
    EntityDAO<Foo<Bar>> fooBarDAO = new EntityDAO<>(Foo.class);

    // dao perfectly generic for Foo<Bar>
    Foo<Bar> entity = fooBarDAO.get(1);
}

; @Japhei在他的评论(可与原始代码一起使用)中指出,但并非总是一个虚拟实例,很容易创建或可用。

With any workaround using Class<Foo<Bar>> type; as a variable the <Bar> is lost at runtime but still we can achieve the EntityDAO class to be as generic to support Foo<Bar> for easier code allow type to extend T inside the constructor / method and cast like:

@SuppressWarnings("unchecked")
public <V extends T> TestDAO(final Class<V> type)
{
    this.type = (Class<T>) type;
}

Then use it:

void static main(String[] args)
{
    // <Bar> information lost at runtime but not important
    EntityDAO<Foo<Bar>> fooBarDAO = new EntityDAO<>(Foo.class);

    // dao perfectly generic for Foo<Bar>
    Foo<Bar> entity = fooBarDAO.get(1);
}

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.

挥剑断情 2025-02-20 23:18:41

如果您有此类型的实例,则可以将其投入到特定类:

Foo<Bar> foo = new Foo<>();
type = (Class<Foo<Bar>>) foo.getClass();

If you have an instance of this type you could cast it to the specific class:

Foo<Bar> foo = new Foo<>();
type = (Class<Foo<Bar>>) foo.getClass();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文