繁体   English   中英

如果 Java 中有两个同名的方法,如何告诉编译器我将使用什么方法?

[英]How to tell compiler what method I am going to use if have two methods with the same names in Java?

如果我导入了 static 方法,如何使用java.lang.Math class 中的方法。 我知道我可以只写 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));
  }

}

如果您想使用自定义方法abs ,请使用Lesson1.abs(-12)调用它。 如果你想使用java.lang.Math.abs调用它Math.abs(-12)或直接abs(-12)导致 static 导入。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM