首先让我为这个糟糕的标题道歉,但我不知道如何用一句话来总结这一点。
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
}
}
}
在上面的代码(OutputStream 只是一个示例)中,方法第一行中的类GarbageWriterExecutor
会导致编译错误,而第二行则不会。关于这个问题,我有两个问题。
- 为什么
stream.getInput()
会导致错误,即使已知TypedStream.I
会扩展OutputStream
? - 如何在没有丑陋的选角的情况下解决这个问题?
因为你的方法
public void writeTrash(TypedStream stream)
还应确保定义了TypedStream
类型,如下所示:
public void writeTrash(TypedStream<I> stream)
编辑:托马斯的回答实际上解释了为什么
TypedStream 流将禁用泛型类型检查,因此编译器只知道 getInput() 将返回一个对象,因此会出现错误。
TypedStream stream
将禁用泛型类型检查,因此编译器只知道getInput()
将返回一个对象,因此会出现错误。
请尝试writeTrash(TypedStream<I> stream)
。
也许您想使用writeTrash(TypedStream<? extends I> stream)
以便能够传递为I
的I
或子类参数化的任何TypedStream
。
另一种选择是
public class GarbageWriterExecutor extends GarbageWriter<OutputStream> {
public void writeTrash(TypedStream<?> stream) throws Exception{
this.writeGarbage(stream.getInput());
}
}
或
public class GarbageWriterExecutor extends GarbageWriter<OutputStream> {
public void writeTrash(TypedStream<? extends OutputStream> stream) throws Exception{
this.writeGarbage(stream.getInput());
}
}
只需使用:
public class GarbageWriterExecutor<I extends OutputStream> extends GarbageWriter<I> {
public void writeTrash(TypedStream<I> stream) throws Exception {
this.writeGarbage(stream.getInput());
}
}
即用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.