我想这样做:
Foo<String> foo = new Foo<>();
Foo<String>.Bar fooBar = foo.new Bar();
fooBar.doSomething("this works!");
但是前两行在一行中,例如:
Foo<String>.Bar fooBar2 = new Foo<>().new Bar();
fooBar2.doSomething("The above line gives: incompatible types. Required: Foo.Bar Found: Foo.Bar");
但是第一行失败了。我得到:
incompatible types.
Required: Foo.Bar
Found: Foo.Bar
这是为什么?
public class Foo<T> {
public class Bar extends SomeOtherClass<T> {
public void doSomething(T t) {
}
}
}
最后一节课:
public class SomeOtherClass<T> {
}
最佳答案
在某些情况下,Java 无法很好地处理类型推断(当人们认为它应该处理时)。这是其中一种情况。
如果您显式地将类型传递给 new
调用,它将编译:
Foo<String>.Bar fooBar2 = new Foo<String>().new Bar();
关于java - 所需类型与找到的类型相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30654656/