且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java相当于python“dir"?

更新时间:2022-01-15 15:28:20

标准库中没有任何东西可以完全完成 dir() 所做的事情,但是你可以得到使用 java.lang.reflect 获取相同的信息.具体来说,调查和发现类的成员在关于发现类的文档中进行了说明会员.使用该 API,您可以轻松地找出您需要了解的有关类属性的信息.

There's nothing in the standard library that accomplishes exactly what dir() does, but you can get the same information using java.lang.reflect. Specifically, investigating and discovering members of a class is explained in the documentation on discovering class members. Using that API you can easily find out what you need to know about the attributes of a class.

实际上,自己实现 dir() 只是定义一个方法来内省类的方法和字段,然后组合信息集合或打印您的任何信息想知道.

In fact, implementing dir() yourself would just be a matter of defining a method that introspects the methods and fields of a class and either assembles a collection of the info or prints whatever information you'd like to know.

dir() 在 Java 中的用处有限,因为 Java 不是交互式的,但是如果您出于教育/调查目的或应用程序逻辑需要它,反射 API 总是存在的.

dir() has limited usefulness in Java because Java is not interactive, but if you need it for educational/investigative purposes or for application logic, the reflection API is always there.