為什么類型推斷在這種特殊情況下不起作用?public class ClassB<O> {public <T> T echo(T msg) { return msg;}public void doSomething(O a) {}public static void main() { // Test 1 ClassB<Object> sampleB = new ClassB<>(); // No compile time type error. // <T> and <O> are different. String test = sampleB.echo(""); // Test 2 ClassB sampleB1 = new ClassB(); // Causes compile time type error. String test2 = sampleB1.echo("");}}方法返回類型 <T> 與 <O> 有什么關(guān)系?編譯器是否將其視為String test2 = (Object) sampleB1.echo("");代替String test2 = (String) sampleB1.echo("");
使用泛型方法進(jìn)行 Java 類型擦除
翻翻過(guò)去那場(chǎng)雪
2023-08-09 17:13:35