[英]Passing generic types as generic method parameters in Java
First let me apologize for the terrible title, but I had no idea how to summarize this in a single sentence. 首先让我为可怕的头衔道歉,但我不知道如何用一句话来总结这一点。
public class GenericFun {
public class TypedStream<I extends OutputStream> {
I input;
public I getInput() { return input; }
public void setInput(I input) { this.input = input; }
}
public abstract class GarbageWriter<I extends OutputStream> {
public void writeGarbage(I output) throws Exception {
output.write("Garbage".getBytes());
}
}
public class GarbageWriterExecutor<I extends OutputStream> extends GarbageWriter<I> {
public void writeTrash(TypedStream stream) throws Exception{
this.writeGarbage(stream.getInput()); // Error
this.writeGarbage((I)stream.getInput()); // OK
}
}
}
In the above code (the OutputStream is just an example) in class GarbageWriterExecutor
class in the method first line causes compilation error, while the second one don't. 在上面的代码(OutputStream只是一个例子)中,类GarbageWriterExecutor
类在方法第一行导致编译错误,而第二行则没有。 I have two questions regarding this. 我有两个问题。
stream.getInput()
causes error, even though TypedStream.I
is known to extend OutputStream
? 为什么stream.getInput()
会导致错误,即使已知TypedStream.I
扩展OutputStream
? Because your method 因为你的方法
public void writeTrash(TypedStream stream)
should also make sure TypedStream
type is defined, like this : 还应该确保定义了TypedStream
类型,如下所示:
public void writeTrash(TypedStream<I> stream)
Edit : Thomas answer actually explain why 编辑:托马斯答案实际解释了原因
TypedStream stream will disable generic type checking and thus the compiler only knows that getInput() will return an object, hence the error. TypedStream流将禁用泛型类型检查,因此编译器只知道getInput()将返回一个对象,因此错误。
TypedStream stream
will disable generic type checking and thus the compiler only knows that getInput()
will return an object, hence the error. TypedStream stream
将禁用泛型类型检查,因此编译器只知道getInput()
将返回一个对象,因此错误。
Try writeTrash(TypedStream<I> stream)
instead. 尝试使用writeTrash(TypedStream<I> stream)
。
Maybe you event want to use writeTrash(TypedStream<? extends I> stream)
in order to be able to pass any TypedStream
which is parameterized for I
or subclasses of I
. 也许你的事件要使用writeTrash(TypedStream<? extends I> stream)
,以便能够通过任何TypedStream
这是对参数I
或子类I
。
Yet another alternative would be 还有另一种选择
public class GarbageWriterExecutor extends GarbageWriter<OutputStream> {
public void writeTrash(TypedStream<?> stream) throws Exception{
this.writeGarbage(stream.getInput());
}
}
or 要么
public class GarbageWriterExecutor extends GarbageWriter<OutputStream> {
public void writeTrash(TypedStream<? extends OutputStream> stream) throws Exception{
this.writeGarbage(stream.getInput());
}
}
Simply use: 只需使用:
public class GarbageWriterExecutor<I extends OutputStream> extends GarbageWriter<I> {
public void writeTrash(TypedStream<I> stream) throws Exception {
this.writeGarbage(stream.getInput());
}
}
Ie parameterize your TypedStream
parameter with I
. 即用I
参数化你的TypedStream
参数。
1.Resolve this by using this code.
public void writeTrash(TypedStream<I> stream) throws Exception{
this.writeGarbage(stream.getInput());
this.writeGarbage(stream.getInput());
}
2. In Generic class class name is followed by a type parameter section. If you are not doing this then you have to do casting.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.