Java 泛型 - 多次使用相同的通配符

发布于 2024-11-01 17:56:39 字数 547 浏览 0 评论 0原文

我有一个使用泛型和有界通配符的类声明:

class Factory<T extends Logic<? extends Entity>, 
              U extends DAO<? extends Entity>> 
{
}

基本上它是一个泛型工厂,它采用逻辑接口 (T) 并返回配置的实现。为了实例化逻辑,我采用了一个适当的 DAO 类来实现 DAO 接口 (U)。

逻辑接口和 DAO 接口都是通用的,并采用要使用的实体的类型作为其类型参数。但是,我想进一步限制这一点,以便 DAO 和 Logic 不仅具有扩展实体的类型参数,而且它们扩展相同的实体。结果可能类似于:

class <X extends Entity> Factory<T extends Logic<X>, 
              U extends DAO<X>> 
{
}

Can I get that with java generics?

I have a class declaration which uses generics and bounded wildcards:

class Factory<T extends Logic<? extends Entity>, 
              U extends DAO<? extends Entity>> 
{
}

Basically its a generic factory, which takes a logic interface (T) and returns a configured implementation. In order to instantiate the logic, I take a appropriate DAO class implementing the DAO interface (U).

Both interfaces for logic and DAO are generic as well and take the type of the entity to work with as their type parameter. However, I want to constrain that further, so that DAO and Logic not only have a type parameter which extends Entity, but that they extend the same Entity. The result may look similiar to that:

class <X extends Entity> Factory<T extends Logic<X>, 
              U extends DAO<X>> 
{
}

Can I achieve that with java generics?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

痴意少年 2024-11-08 17:56:39

是的,你很接近。这样做:

class Factory<X extends Entity,
              T extends Logic<X>, 
              U extends DAO<X>> 
{
}

替代方案

class Factory<T extends Logic<?>, 
              U extends DAO<?>> 
{
    // Here, the generic method parameter only requires X
    // to be the same bound at method invocation. However,
    // you will "lose" that information again when the 
    // Factory is returned.
    public static <X extends Entity,
                   T extends Logic<X>, 
                   U extends DAO<X>> Factory<T, U> createFactory(T logic, U dao)
    {
        return new Factory<T, U>(logic, dao);
    }
}

Yes, you're close. Do it like this:

class Factory<X extends Entity,
              T extends Logic<X>, 
              U extends DAO<X>> 
{
}

Alternative

class Factory<T extends Logic<?>, 
              U extends DAO<?>> 
{
    // Here, the generic method parameter only requires X
    // to be the same bound at method invocation. However,
    // you will "lose" that information again when the 
    // Factory is returned.
    public static <X extends Entity,
                   T extends Logic<X>, 
                   U extends DAO<X>> Factory<T, U> createFactory(T logic, U dao)
    {
        return new Factory<T, U>(logic, dao);
    }
}
零度° 2024-11-08 17:56:39

另一种方法可能是提供一个包装器(尽管这并不是很优雅;)):

class Entity{}

interface Logic<T extends Entity> {}

interface DAO<T extends Entity> {}

interface DaoLogic<X extends Entity> {
  DAO<X> getDAO();
  Logic<X> getLogic();
}

class Factory<T extends DaoLogic<? extends Entity>> {}

Another approach could be to provide a wrapper (although that's not really elegant ;) ):

class Entity{}

interface Logic<T extends Entity> {}

interface DAO<T extends Entity> {}

interface DaoLogic<X extends Entity> {
  DAO<X> getDAO();
  Logic<X> getLogic();
}

class Factory<T extends DaoLogic<? extends Entity>> {}
谎言月老 2024-11-08 17:56:39

会进行以下工作吗? X 将是“通用”类型,其中 Logic 和 DAO 都将使用此类型。

public class Factory<X extends Entity, T extends Logic<X>, U extends DAO<X>>
{
}

Would the following work. X would be the "common" type, where Logic and DAO both would use this type.

public class Factory<X extends Entity, T extends Logic<X>, U extends DAO<X>>
{
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文