簡體   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