[英]How to tell compiler what method I am going to use if have two methods with the same names in Java?
How to use a method from java.lang.Math
class if I imported static methods.如果我导入了 static 方法,如何使用java.lang.Math
class 中的方法。 I know I can just write Math.abs(-12), but are there other ways to do this?我知道我可以只写 Math.abs(-12),但是还有其他方法可以做到这一点吗?
import static java.lang.Math.*;
public class Lesson1 {
private static int abs(int x) {
System.out.println("My abs method");
return x;
}
public static void main(String[] args) {
System.out.println(abs(-12));
}
}
If you want to use your custom method abs
, call it with Lesson1.abs(-12)
.如果您想使用自定义方法abs
,请使用Lesson1.abs(-12)
调用它。 If you want to use java.lang.Math.abs
call it with Math.abs(-12)
or directly abs(-12)
cause the static import.如果你想使用java.lang.Math.abs
调用它Math.abs(-12)
或直接abs(-12)
导致 static 导入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.