我尝试从我的Main获取foo():
<T extends Main> T foo()
{
return this; // "this" it is a instance of Main
}
Run Code Online (Sandbox Code Playgroud)
并使用它
Main m = new Main();
Main m2 = m.foo();
Run Code Online (Sandbox Code Playgroud)
我在方法foo()中遇到错误:" 不兼容的类型.必需的T,找到主要 ".
我知道我可以用这个:
<T extends Main> T foo2(T a) {
return a;
}
Run Code Online (Sandbox Code Playgroud)
并使用它:
Main m = new Main();
Main m3 = m.foo2(m);
Run Code Online (Sandbox Code Playgroud)
它工作正常.但我无法理解,为什么我不能使用第一种方法?因为this
绝对是Main的实例扩展.