为什么这种类型不是类型参数的有效替代品? [英] Why is this type not a valid substitute for the type parameter?

查看:180
本文介绍了为什么这种类型不是类型参数的有效替代品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用泛型来支持委托对象(装饰器,包装器)的可配置结构。我想要构建一个实现目标接口的代理链,以及一个通用的委托接口。

我有这个提纲:

  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.Delegator< T>>
Test.DelegatorChain< T> code>


我承认泛型就像 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 type Test.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆