我在同一个项目中有 2 个类,并且我在这两个类中都使用了 Math.random() 。
在第一个中,一切正常,但在第二个中,它说 random()
未定义。
“Math 类型的方法 random()
未定义”
有什么解决办法吗?
最佳答案
你必须打电话
java.lang.Math.random()
因为你的类名也是Math,所以你必须指定package。
示例:
public class Math {
public static void main(String args []){
System.out.println("JDK MATH RANDOM " +java.lang.Math.random()); // refers to java.lang
System.out.println("My Math random implementation "+Math.random()); // refers to this class method, actually Math is redundant in this scope
}
public static double random(){
//some implementation
}
}
关于java - Java 中的 Math.random(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17459795/