简体   繁体   English

Java递归通用和通配符

[英]Java recursive Generic and Wildcard

Hello i have a question about Java and using wildcard with recursive generics. 您好我有一个关于Java的问题,并使用带有递归泛型的通配符。 I have the following interface 我有以下界面

public interface Recursive<R extends Recursive<R>> {}

and another intrface which uses the recursive interface 和另一个使用递归接口的插件

public interface Use<R extends Recursive<R>> {}

now i have interface which looks like this 现在我的界面看起来像这样

public interface Base<R extends Recursive<R>> {
    Use<R>[] getUses();
}

and based on that class i want a derived class which uses a wildcard like that 并且基于该类我想要一个使用这样的通配符的派生类

public class Derived extends Base<Recursive<?>> {
    Use<?>[] getUses();
}

which isnt allowed but how can i manage to say that my generic type is everything that fits? 这是不允许的,但我怎么能设法说我的通用类型是适合的一切?

The problem is that Recursive< ? > 问题是Recursive< ? > Recursive< ? > doesn't satisfy the constraint on Base 's type parameter. Recursive< ? >不满足Base类型参数的约束。

If you want to extend Base< R > you have to supply something for R which is also a Recursive< R > . 如果你想扩展Base< R >你必须为R提供一些东西,它也是一个Recursive< R > Recursive< ? > Recursive< ? > is not good enough because the compiler can't figure out that Recursive< ? > extends Recursive< Recursive< ? > > Recursive< ? >不够好,因为编译器无法弄清楚Recursive< ? > extends Recursive< Recursive< ? > > Recursive< ? > extends Recursive< Recursive< ? > > Recursive< ? > extends Recursive< Recursive< ? > > . Recursive< ? > extends Recursive< Recursive< ? > >

It doesn't really make sense to extend a Base of unknown type, but you can declare a heterogeneous collection of them: 扩展未知类型的Base是没有意义的,但是您可以声明它们的异构集合:

Collection< Base< ? > > cb; // ok for some reason
Collection< Base< Recursive< ? > > > cbr; // not ok!

From your comment we know Base looks like 从您的评论我们知道Base看起来像

public abstract class Base< R extends Recursive< R > > {
    public abstract R[] foo();
}

For each qualifying R you can of course do 对于每个符合资格的R你当然可以

public class Derived< R extends Recursive< R > > extends Base< R > {
    @Override public R[] foo() {
        return ( R[] )new Object[] { r1, r2 };
    }
}

but it's not clear from your comment that this is what you want. 但是你的评论并不清楚这是你想要的。 Do you know what the specific R is that you want Derived to use? 你知道你想要Derived使用的特定R是什么吗?

EDIT the following compiles. 编辑以下编译。 would that suffice? 那还够吗?

interface Recursive< R extends Recursive< R > > {}
abstract class Base< R extends Recursive< ? extends R > > {
    abstract public R[] foo();
}
class Derived extends Base< Recursive< ? > > {
    public Recursive< ? >[] foo() { return null; }
}

EDIT 2 Adjusting for the change to the original post: 编辑2调整原始帖子的更改:

interface Recursive< R extends Recursive< R > > {}
interface Use< R extends Recursive< ? extends R > > {}
abstract class Base< R extends Recursive< ? extends R > > {
    abstract public Use< R >[] foo();
}
class Derived extends Base< Recursive< ? > > {
    @Override public Use< Recursive< ? > >[] foo() { return null; }
}

You need to type (with the right bound) your subclass, otherwise the compiler can't assert that the wildcard of the class is the same type as what your method returns, which is required. 您需要键入(使用右边界)您的子类,否则编译器无法断言该类的通配符与您的方法返回的类型相同 ,这是必需的。

Try this: 尝试这个:

public class Derived<R extends Recursive<R>> extends Base<R> {
    Use<R>[] getUses();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM