我反编译了一个java .class文件并看到了这行代码
new ResponseModel("Reset Complete", false, (LinkedHashMap)null)
该行对应于
new ResponseModel("Reset Complete", false, null);
为什么输入null参数?它只是我的反编译器暗示参数类型?
想象你已经重载了一个方法:
public class Foo {
public void something (String s) { ... }
public void something (List l) { ... }
}
使用something
参数调用null
现在是模棱两可的。要将调用绑定到任一方法,您需要转换null值,为其指定一个类型:
new Foo().something((String)null);
new Foo().something((List)null);
由于此类在运行时可能与编译时不同(在编译时,方法可能不会重载,但在运行时该类是一个具有重载方法的较新版本),编译器在字节码中使其显式化以防止后来模棱两可。