具有两个泛型的泛型出现意外绑定错误
Unexpected bound error on Generics with two generics
本质上我想创建一个接口和 classes 的结构,在不同的级别我想强制某种类型。在下面的示例中,我定义了一个名为 Engine
和 Body
的接口。我希望所有 Car
classes 都由 Body
和 Engine
组成。然而,在我到达特定的汽车 class 例如 MySportCar
之前,我想创建汽车的子类型,例如 SportCar
其中 SportCar
必须有 V6
引擎怎么样Body
。但是,当我尝试这样做时,出现意外的绑定错误。我的代码是:
public interface Body {
int getNumberOfDoors();
}
public class Sedan implements Body {
@Override
public int getNumberOfDoors() {
return 4;
}
}
public interface Engine {
int getHorsePower();
}
public class V6 implements Engine{
@Override
public int getHorsePower() {
return 500;
}
}
然后我创建 Car
摘要 class:
public class Car <B extends Body, E extends Engine>{
private B body;
private E engine;
public Car(B body, E engine) {
this.body = body;
this.engine = engine;
}
public B getBody() {
return body;
}
public E getEngine() {
return engine;
}
}
理想情况下,我想要 SportCar
这样的东西,但它因无限错误而失败:
public class SportCar extends Car<B extends Body, V6>{
public SportCar(B body, V6 engine) {
super(body, engine);
}
}
当然以后我会创建自己的 SportCar
public class MySportCar extends SportCar<Sedan, V6>{
public MySportCar(Sedan body, V6 engine) {
super(body, engine);
}
}
但目前的问题是编译器不喜欢我对 SportCar
的 class 定义。特别是导致意外绑定错误的代码 B extends Body
。
如有任何帮助,我们将不胜感激。
#解决方案
我想这就是你需要的...
// Change is here
class SportCar<B extends Body, V extends V6> extends Car<B, V> {
public SportCar (B body, V6 engine) {
super (body, engine);
}
}
// No change here
class MySportCar extends SportCar<Sedan, V6> {
public MySportCar (Sedan body, V6 engine) {
super (body, engine);
}
}
错在这里
class SportCar extends Car<B extends Body, V6> {
是你使用的是B
类型而没有声明。您有一个未声明的泛型类型,您必须首先使用
声明它
class SportCar<B extends Body, V extends V6> extends Car<B, V> {
// No error here
我假设您希望 SportCar
只允许 Body
和 V6
潜艇。这些也是 Car
的边界,因为它需要 Body
和 Engine
以及 V6
实现引擎。
所以如果我的假设是正确的,你想绑定 SportCar
只允许 Body
和 V6
的子项,那么你就可以开始了......
本质上我想创建一个接口和 classes 的结构,在不同的级别我想强制某种类型。在下面的示例中,我定义了一个名为 Engine
和 Body
的接口。我希望所有 Car
classes 都由 Body
和 Engine
组成。然而,在我到达特定的汽车 class 例如 MySportCar
之前,我想创建汽车的子类型,例如 SportCar
其中 SportCar
必须有 V6
引擎怎么样Body
。但是,当我尝试这样做时,出现意外的绑定错误。我的代码是:
public interface Body {
int getNumberOfDoors();
}
public class Sedan implements Body {
@Override
public int getNumberOfDoors() {
return 4;
}
}
public interface Engine {
int getHorsePower();
}
public class V6 implements Engine{
@Override
public int getHorsePower() {
return 500;
}
}
然后我创建 Car
摘要 class:
public class Car <B extends Body, E extends Engine>{
private B body;
private E engine;
public Car(B body, E engine) {
this.body = body;
this.engine = engine;
}
public B getBody() {
return body;
}
public E getEngine() {
return engine;
}
}
理想情况下,我想要 SportCar
这样的东西,但它因无限错误而失败:
public class SportCar extends Car<B extends Body, V6>{
public SportCar(B body, V6 engine) {
super(body, engine);
}
}
当然以后我会创建自己的 SportCar
public class MySportCar extends SportCar<Sedan, V6>{
public MySportCar(Sedan body, V6 engine) {
super(body, engine);
}
}
但目前的问题是编译器不喜欢我对 SportCar
的 class 定义。特别是导致意外绑定错误的代码 B extends Body
。
如有任何帮助,我们将不胜感激。
#解决方案
我想这就是你需要的...
// Change is here
class SportCar<B extends Body, V extends V6> extends Car<B, V> {
public SportCar (B body, V6 engine) {
super (body, engine);
}
}
// No change here
class MySportCar extends SportCar<Sedan, V6> {
public MySportCar (Sedan body, V6 engine) {
super (body, engine);
}
}
错在这里
class SportCar extends Car<B extends Body, V6> {
是你使用的是B
类型而没有声明。您有一个未声明的泛型类型,您必须首先使用
class SportCar<B extends Body, V extends V6> extends Car<B, V> {
// No error here
我假设您希望 SportCar
只允许 Body
和 V6
潜艇。这些也是 Car
的边界,因为它需要 Body
和 Engine
以及 V6
实现引擎。
所以如果我的假设是正确的,你想绑定 SportCar
只允许 Body
和 V6
的子项,那么你就可以开始了......