java中泛型类型的组合。

时间:2021-12-27 22:04:19

Let's say I'm defining a class User, which has a generic type which extends Principal

假设我定义了一个类用户,它有一个扩展主体的泛型类型。

public abstract class User<P extends Principal> {
}

And I have an action base class which should be parameterized with an user:

我有一个action基类,它应该和一个用户参数化:

public abstract class Action<U extends User<Principal>> {
}

Those two classes are on muy codebase. Now, I want to use them on an specific application, so I create an implementation for Principal and User:

这两个类在muy代码库中。现在,我想在一个特定的应用程序上使用它们,因此我为Principal和User创建了一个实现:

public class MyPrincipal implements Principal {
    public String getName() {
        return "Administrator";
    }
}

public class MyUser extends User<MyPrincipal>{
}

Once I have my user, I want to use it on an action

一旦我有了我的用户,我想用它在一个行动上。

public class MyAction extends Action<MyUser>{
}

This last class does not compile. It complais that :

最后一个类不能编译。它complais:

Bound mismatch: The type MyUser is not a valid substitute for the bounded parameter <U extends User<Principal>> of the type Action<U>

What I want is to enforce that all classes extending MyAction should be parameterized with an object that extends User, and also is parameterized by an object which implements Principal. What I'm missing?

我想要的是强制所有扩展MyAction的类都应该用一个扩展用户的对象来参数化,并且还可以由实现主体的对象参数化。我错过什么?

2 个解决方案

#1


3  

Currently, the bound on your Action class says - U extends User<Principal>. Since MyUser deoesn't extend a User<Principal>, but a User<MyPrincipal>, it won't work as you expected. A User<MyPrincipal> is not a User<Principal> (distinct types).

当前,Action类的绑定表示- U扩展用户 。由于MyUser deoesn不扩展用户 ,但是用户 ,它不会像您预期的那样工作。用户 不是用户 <主要> (不同类型)。

You can introduce another type parameter in your Action class, and use that in bounds of U:

您可以在Action类中引入另一个类型参数,并在U的范围内使用该参数:

public abstract class Action<P extends Principal, U extends User<P>> {
}

and now, you can create your MyAction like this:

现在,你可以像这样创建你的MyAction:

public class MyAction extends Action<MyPrincipal, MyUser>{
}

Or you can also modify your class using wildcard bound:

或者您也可以使用通配符来修改您的类:

public abstract class Action<U extends User<? extends Principal>> {
}

This way you won't need another type parameter:

这样您就不需要另一个类型参数:

public class MyAction extends Action<MyUser>{
}

References:

引用:

#2


2  

Change Action's signature to this (I tested this in Eclipse and it compiles):

更改操作的签名(我在Eclipse中测试了它并编译):

public abstract class Action<U extends User<? extends Principal>>

By doing this, you're saying that any type of Principal is okay, not just base-type Principal objects (which of course can't be instantiated).

通过这样做,您会说任何类型的主体都是可以的,而不仅仅是基本类型的主体对象(当然不能实例化)。

#1


3  

Currently, the bound on your Action class says - U extends User<Principal>. Since MyUser deoesn't extend a User<Principal>, but a User<MyPrincipal>, it won't work as you expected. A User<MyPrincipal> is not a User<Principal> (distinct types).

当前,Action类的绑定表示- U扩展用户 。由于MyUser deoesn不扩展用户 ,但是用户 ,它不会像您预期的那样工作。用户 不是用户 <主要> (不同类型)。

You can introduce another type parameter in your Action class, and use that in bounds of U:

您可以在Action类中引入另一个类型参数,并在U的范围内使用该参数:

public abstract class Action<P extends Principal, U extends User<P>> {
}

and now, you can create your MyAction like this:

现在,你可以像这样创建你的MyAction:

public class MyAction extends Action<MyPrincipal, MyUser>{
}

Or you can also modify your class using wildcard bound:

或者您也可以使用通配符来修改您的类:

public abstract class Action<U extends User<? extends Principal>> {
}

This way you won't need another type parameter:

这样您就不需要另一个类型参数:

public class MyAction extends Action<MyUser>{
}

References:

引用:

#2


2  

Change Action's signature to this (I tested this in Eclipse and it compiles):

更改操作的签名(我在Eclipse中测试了它并编译):

public abstract class Action<U extends User<? extends Principal>>

By doing this, you're saying that any type of Principal is okay, not just base-type Principal objects (which of course can't be instantiated).

通过这样做,您会说任何类型的主体都是可以的,而不仅仅是基本类型的主体对象(当然不能实例化)。