简体   繁体   English

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

[英]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.

相关问题 JAVA:具有相同名称的方法? - JAVA: Methods that have same names? Java编译器:具有相同名称和不同签名的两个方法如何匹配方法调用? - Java compiler: How can two methods with the same name and different signatures match a method call? 当我们在Java中有两个具有相同名称的类时会发生什么? - what happens when we have two classes with same names in java? 如何告诉Java两个“?”是同一类型? - How can I tell Java that two “?”s are the same type? 如何使用JUnit测试两个都具有Java主要方法的类(客户端和服务器)? - How do I use JUnit to test two classes (a client and a server) that both have main methods in Java? 如何对两种操作方法使用相同的验证方法 - How to use same validation method for two action methods 如何告诉编译器两个对象是相同的(但未知)类(带有泛型)? - How to tell compiler that two objects are of same - but unknown - class (with generics)? 我有两种几乎相同的方法,如何重构它们? - I have two methods which are nearly the same, how to refactor them? 如何在java中使用两个绘制方法?或者在基本涂料方法之外 - How to use two paint methods in java? Or outside the basic paint method 如何告诉Java两个通配符类型相同? - How to tell Java that two wildcard types are the same?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM