为什么这种类型不是类型参数的有效替代品? [英] Why is this type not a valid substitute for the type parameter?
问题描述
我正在尝试使用泛型来支持委托对象(装饰器,包装器)的可配置结构。我想要构建一个实现目标接口的代理链,以及一个通用的委托接口。
我有这个提纲:
class Test {
static interface Delegator< T> {}
static class DelegatorChain< T extends Delegator< T>> {}
静态接口Foo {}
静态类FooDelegator实现Delegator< Foo>,Foo {}
public static void main(String [ ] args){
DelegatorChain< FooDelegator> chain = new DelegatorChain< FooDelegator>();
但是,当试图实例化<$ c $
绑定不匹配:类型Test.FooDelegator不是有效的替代对于
类型的有界参数
< T
Test.DelegatorChain< T> code>
扩展Test.Delegator< T>>
我承认泛型就像 magic ,但我可以以某种方式确认FooDelegator不是一个Foo, extends Delegator< Foo>,它只是实现了两者接口。
鉴于我很清楚自己想完成什么,有什么我可以做的泛型来解决它,或者我只是忘了它更好吗?
在您的定义下,Delegator是Delegator (比如Comparable),但是它的意图似乎是Delegator是一个超类的委托人。幸运的是,泛型有一种表达方式:
static class DelegatorChain< T extends Delegator< ;? super T>> {}
这表示Delagator类型必须是T的超类。通过这一改变,您原始代码的其余部分将会被编译:
静态接口Delegator< T> {}
static class DelegatorChain< T extends Delegator< ;? super T>> {}
static interface Foo {}
static class FooDelegator implements Delegator< Foo>,Foo {}
public static void main(String [] args){
DelegatorChain< FooDelegator> chain = new DelegatorChain< FooDelegator>();
}
另外,无论何时您使用通用超级界限,您的代码看起来都很酷: )
注意:以下原本是问题中的第一选项。
还有另一种方法让你的代码编译,但是它比较失败,因为它失去了Delegator类型与它委派的连接:
//不推荐,但可以编译:
static class FooDelegator实现Delegator< FooDelegator>,Foo {}
//然而,这也编译:(
static class FooDelegator实现Delegator< FooDelegator>,Bar {}
I'm experimenting with using generics to support a configurable structure of delegating objects (decorators, wrappers). I want to build a chain of delegators that implements a target interface as well as a generic delegator interface.
I have this outline:
class Test {
static interface Delegator<T> {}
static class DelegatorChain<T extends Delegator<T>> {}
static interface Foo {}
static class FooDelegator implements Delegator<Foo>, Foo {}
public static void main(String[] args) {
DelegatorChain<FooDelegator> chain = new DelegatorChain<FooDelegator>();
}
}
But, when trying to instantiate the chain
variable, compiler complains:
Bound mismatch: The type Test.FooDelegator is not a valid substitute for the bounded parameter
<T extends Test.Delegator<T>>
of the typeTest.DelegatorChain<T>
I admit that generics is like magic to me, but I can somehow acknowledge that FooDelegator is not a Foo that extends Delegator<Foo>, it simply implements both interfaces.
Given that it's clear what I want to accomplish, is there anything I can do w.r.t. generics to fix it, or am I just better of forgetting about it?
Under your definition, a Delegator is a Delegator of itself (like Comparable is for example), however it seems the intention is that Delegator is a Delegator of a super class. Luckily, generics has a way of expressing this:
static class DelegatorChain<T extends Delegator<? super T>> {}
This says that the "Delagator type must be a super class of T". With this change, the rest of your original code compiles:
static interface Delegator<T> {}
static class DelegatorChain<T extends Delegator<? super T>> {}
static interface Foo {}
static class FooDelegator implements Delegator<Foo>, Foo {}
public static void main(String[] args) {
DelegatorChain<FooDelegator> chain = new DelegatorChain<FooDelegator>();
}
Also, anytime you use a generic super bound, your code looks really cool :)
Note: This following was originally the "first option" in the question.
There is another way to get your code to compile, but it is inferior because it loses the connect between the Delegator type and what it's delegating from:
// Not recommended, but will allow compile:
static class FooDelegator implements Delegator<FooDelegator>, Foo {}
// However, this also compiles :(
static class FooDelegator implements Delegator<FooDelegator>, Bar {}
这篇关于为什么这种类型不是类型参数的有效替代品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!