It error's on the "return this;" line with this: Type mismatch: cannot convert from A to T.
public class A<T extends A<T>>{
public T method() {
return this;
}
}
I don't understand why the compiler can't convert from A to T, when A is a suitable candidate for T.
Let us assume your code was valid...
You might have a subclass defined like this
public class B extends A<B> {
}
Here type parameter T
is same as type of this
.
Which is why I guess you said
A is a suitable candidate for T
Now consider this case
public class C extends A<B> {
}
which is perfectly valid since B
passes all criteria required for T
.
In this case your method declaration in class A
becomes invalid.
(return type is B
but you are returning instance of class C
.)
So your assumption that
A is a suitable candidate for T
is invalid. Hence the error.
Hope this helps.
Good luck.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.