【发布时间】:2012-01-31 19:05:59
【问题描述】:
如果您有一个静态导入到java.lang.Integer 的类,而我的类也有一个静态方法parseInt(String),那么调用parseInt("12345") 将指向哪个方法?
提前致谢!
【问题讨论】:
标签: java import method-signature static-import method-declaration
如果您有一个静态导入到java.lang.Integer 的类,而我的类也有一个静态方法parseInt(String),那么调用parseInt("12345") 将指向哪个方法?
提前致谢!
【问题讨论】:
标签: java import method-signature static-import method-declaration
如果您在自己的班级中,它将调用 您的 方法。
如果您不在您的班级(并导入两个班级),则必须指定要使用的班级。
证明:http://java.sun.com/docs/books/jls/download/langspec-3.0.pdf $8 和 $6.3(见 cmets)
【讨论】:
A declaration d of a method named n shadows the declarations of any other methods named n that are in an enclosing scope at the point where d occurs throughout the scope of d.
试试这个:
import static java.lang.Integer.parseInt;
public class Test {
public static void main(String[] args) {
System.out.println(parseInt("12345"));
}
private static int parseInt(String str) {
System.out.println("str");
return 123;
}
}
结果:
str
123
类中的方法首先执行。
【讨论】: