java 为什么空投参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/315846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why null cast a parameter?
提问by pek
When and why would somebody do the following:
何时以及为何有人会执行以下操作:
doSomething( (MyClass) null );
Have you ever done this? Could you please share your experience?
你有没有这样做过?你能分享一下你的经验吗?
回答by Johannes Schaub - litb
If doSomethingis overloaded, you need to cast the null explicitly to MyClassso the right overload is chosen:
如果doSomething重载,您需要显式地将 null 强制转换为,MyClass以便选择正确的重载:
public void doSomething(MyClass c) {
// ...
}
public void doSomething(MyOtherClass c) {
// ...
}
A non-contrived situation where you need to cast is when you call a varargs function:
当您调用 varargs 函数时,您需要进行强制转换的一种非人为的情况:
class Example {
static void test(String code, String... s) {
System.out.println("code: " + code);
if(s == null) {
System.out.println("array is null");
return;
}
for(String str: s) {
if(str != null) {
System.out.println(str);
} else {
System.out.println("element is null");
}
}
System.out.println("---");
}
public static void main(String... args) {
/* the array will contain two elements */
test("numbers", "one", "two");
/* the array will contain zero elements */
test("nothing");
/* the array will be null in test */
test("null-array", (String[])null);
/* first argument of the array is null */
test("one-null-element", (String)null);
/* will produce a warning. passes a null array */
test("warning", null);
}
}
The last line will produce the following warning:
最后一行将产生以下警告:
Example.java:26: warning: non-varargs call of varargs method with inexact argument type for last parameter;
cast tojava.lang.Stringfor a varargs call
cast tojava.lang.String[]for a non-varargs call and to suppress this warning
Example.java:26: 警告:最后一个参数的参数类型不准确的可变参数方法的非可变参数调用;
强制转换java.lang.String为可变参数调用
强制转换java.lang.String[]为非可变参数调用并抑制此警告
回答by Eren Aygunes
Let's say you have these two functions, and assume that they accept nullas a valid value for the second parameters.
假设您有这两个函数,并假设它们接受null作为第二个参数的有效值。
void ShowMessage(String msg, Control parent);void ShowMessage(String msg, MyDelegate callBack);
void ShowMessage(String msg, Control parent);void ShowMessage(String msg, MyDelegate callBack);
These two methods differ only by the type of their second parameters. If you want to use one of them with a nullas the second parameter, you must cast the nullto the type of second argument of the corresponding function, so that compiler can decide which function to call.
这两种方法的区别仅在于它们的第二个参数的类型。如果你想使用其中一个 anull作为第二个参数,你必须将 thenull强制转换为对应函数的第二个参数的类型,以便编译器可以决定调用哪个函数。
To call the first function: ShowMessage("Test", (Control) null);
For the second: ShowMessage("Test2", (MyDelegate) null);
调用第一个函数:ShowMessage("Test", (Control) null);
对于第二个:ShowMessage("Test2", (MyDelegate) null);

