Java equivalent to python “dir”?

前端 未结 2 526
[愿得一人]
[愿得一人] 2021-01-19 21:49

Is there an equivalent to \"dir\" in python for java, or a library which provides similar functionality (i.e. properties of objects and classes output as informative strings

2条回答
  •  [愿得一人]
    2021-01-19 22:17

    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.

    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() 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.

提交回复
热议问题