我只是试图通过阅读源代码来理解 api 的一些代码。这是一个链接:
在这个类中,您可以找到方法public int getInt(String path, int def)
。此方法调用 toInt(val)
。我在哪里可以找到这个方法。由于没有指定对象或类,例如 anObject.toInt(val)
或 ClassName.toInt(val)
,因此必须在该类或父类(super class)中定义该方法,但我找不到它。
我的问题:这是原始来源吗?你能找到吗?它在哪里?
最佳答案
toInt()
方法来自 org.bukkit.util.NumberConversions
类。
现在,为什么没有指定类,这是如何工作的?如果您查看文件顶部的导入,您将看到:
import static org.bukkit.util.NumberConversions.*;
这基本上意味着
Make available to me any public static method in in the
org.bukkit.util.NumberConversions
class.
当你想让你的代码更简洁时,这是 Java 的一个有用的特性。然而,由于负责此方法的类不是很明显,因此最好只将它用于广泛用户的辅助方法,例如此处的 toInt
。
另一个典型的例子是 JUnit 断言。甚至有解释in their javadoc :
These methods can be used directly: Assert.assertEquals(...), however, they read better if they are referenced through static import:
import static org.junit.Assert.*; ... assertEquals(...);
关于Java:方法在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33970860/