English 中文(简体)
Java 通用: 非法的前瞻
原标题:Java generics: Illegal forward reference

鉴于通用接口

interface Foo<A, B> { }

我想写的是需要A级成为B级的文字。 因此,我希望这样做。

class Bar<A, B super A> implements Foo<A, B> { }
// --> Syntax err或

class Bar<A extends B, B> implements Foo<A, B> { }
// --> illegal f或ward reference

But the only solution that seems to w或k is this:

class Bar<B, A extends B> implements Foo<A, B> { }

which is kind of ugly, because it reverses the 或der of the generic parameters.
Are there any solutions 或 w或karounds to this problem?

最佳回答

www.un.org/Depts/DGACM/index_spanish.htm 由于 Java可能这样做,试图考虑<代码>。 Bar<B, Aext B> differently.

当你宣布“条码”的变数时,你首先重订母级,然后重新确定子女类别。 如何使用<条码>Bar。 不要将它视为倒退——认为它是向前推进的。 父母应当自然地在子女之前指定。 你补充说的这种额外关系是驱动参数订单的,而不是基本接口。

问题回答

After seeing this question, I spent a little bit trying some different techniques that I thought might work. For example, building a generic interface ISuper<B,A extends B> and then having Bar<A,B> implements ISuper<B,A> (and a similar technique with a sub-class and extends rather than implements) but this just results in a type error, Bar.java:1: type parameter A is not within its bound. Likewise, I tried creating a method private <A extends B> Bar<A,B> foo() { return this; }; and calling it from the constructor, but this just results in the fun type error message Bar.java:2: incompatible types found : Bar<A,B> required: Bar<A,B>

因此,我认为,不幸的是,答案是没有的。 显然,这并不是你想要的答案,但正确的答案似乎是,这只是不可能的。

有人已经指出,既无解决办法,也无ice事。 这是我最后做的。 这只是为我的特殊案例而努力的,但如果你遇到类似问题,你可以把它看作是一种灵感。 (这也是我为什么要处理这个问题的原因)

首先,有这一类(仅指相关接口):

class Pipe<Input, Output> {

    boolean hasNext();

    Input getNext();

    void setNext(Output o);

}

接口实际上为:

interface Processor<Input, Output> {

    process(Pipe<Input, Output> p);

}

和<代码>Bar等同工作

class JustCopyIt<Input, Output> implements Processor<Input, Output> {

    process(Pipe<Input, Output> p) {
       while (p.hasNext()) p.setNext(p.getNext());
    }

}

The easiest way would be to cast the values like this: p.setNext((Output) p.getNext()). But this is bad as it would allow to create an instance of JustCopyIt<Integer, String>. Calling this object would mysteriously fail at some point, but not at the point where the actual error is made.

Doing class JustCopyIt<Type> Implements 加工商和提炼;Type, 类型和特有; 也不会在这里工作,因为当时我无法处理<代码>Pipe<String, Object>。

So what I finally did was to change the interface to this:

interface Processor<Input, Output> {

    process(Pipe<? extends Input, ? super Output> p);

}

这样,JustCopyIt<List>就能够处理Pipe<ArrayList, Collection>

虽然这一技术上似乎是唯一有效的解决办法,但这一解决办法仍然不正确,因为它1)只针对这一特殊案例开展工作,2)要求我改变接口(并非总是可能的),3)使其他处理器的代码变得模糊不清。

Edit:
Reading Keiths answer again inspired me for another solution:

public abstract class Bar<A, B> implements Foo<A, B> {

    public static <B, A extends B> Bar<A, B> newInstance() {
        return new BarImpl<B, A>();
    }

    private static class BarImpl<B, A extends B> extends Bar<A, B> {
        // code goes here
    }

}

// clean code without visible reversed parameters
Bar<Integer, Object> bar1 = Bar.newInstance();
Bar<Object, Integer> bar2 = Bar.newInstance(); // <- compile error




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签