Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
只有一个类型参数的Java嵌套泛型类_Java_Generics - Fatal编程技术网

只有一个类型参数的Java嵌套泛型类

只有一个类型参数的Java嵌套泛型类,java,generics,Java,Generics,我正在从事一个具有通用流接口的项目,该接口提供以下类型的值: interface Stream<T> { T get(); // returns the next value in the stream } 我有一个实现,它只从一个文件或任何东西中提供单个值。看起来是这样的: class SimpleStream<T> implements Stream<T> { // ... } // Doesn't compile class PairStr

我正在从事一个具有通用流接口的项目,该接口提供以下类型的值:

interface Stream<T> {
  T get();  // returns the next value in the stream
}
我有一个实现,它只从一个文件或任何东西中提供单个值。看起来是这样的:

class SimpleStream<T> implements Stream<T> {
  // ...
}
// Doesn't compile
class PairStream<Pair<T>> implements Stream<Pair<T>> {
  // ...
}
我还希望有另一个提供成对值的实现,比如,为每个调用提供接下来的两个值。所以我定义了一个小的Pair类:

class Pair<T> {
  public final T first, second;

  public Pair(T first, T second) {
    this.first = first; this.second = second;
}
现在我想定义流接口的第二个实现,它只适用于Pair类,如下所示:

class SimpleStream<T> implements Stream<T> {
  // ...
}
// Doesn't compile
class PairStream<Pair<T>> implements Stream<Pair<T>> {
  // ...
}
然而,这并不编译

我可以这样做:

class PairStream<U extends Pair<T>, T> implements Stream<U> {
  // ...
}

但是还有更优雅的方式吗?这是正确的方法吗?

泛型类型参数对无效;您只需要在实现接口时声明并使用它

//         Just <T> here;          Added missing (2nd) '>'
class PairStream<T> implements Stream<Pair<T>> {
    public Pair<T> get() { /* ... */ }
}

泛型类型参数对无效;您只需要在实现接口时声明并使用它

//         Just <T> here;          Added missing (2nd) '>'
class PairStream<T> implements Stream<Pair<T>> {
    public Pair<T> get() { /* ... */ }
}

你真正需要的是

class PairStream<T> implements Stream<Pair<T>> {
  // ...
}
这也可能奏效:

class PairStream<U extends Pair<T>> implements Stream<U> {
    // ...
}

你真正需要的是

class PairStream<T> implements Stream<Pair<T>> {
  // ...
}
这也可能奏效:

class PairStream<U extends Pair<T>> implements Stream<U> {
    // ...
}

您在PairStream实现Streamtypo的类中漏掉了一个>:StreamI修复了输入错误;实际上这并不是问题的一部分,您在PairStream实现Streamtypo的类中遗漏了一个>:StreamI修复了错误;这实际上不是问题的一部分。