Java相当于python的“dir”?

发布于 2024-10-21 01:52:07 字数 345 浏览 1 评论 0原文

java中的python中是否有相当于“dir”的东西,或者提供类似功能的库(即对象和类的属性输出为信息字符串)?

这个问题与 clojure 的 问题 类似,并且可能与 Java Reflection 有关在这个问题中,这似乎是关于更复杂但相似的主题。

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)?

This question is similar this question for clojure and probably has something to do with Java Reflection as in this question, which seems to be about a more complicated, but similar, topic.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟雨扶苏 2024-10-28 01:52:07

标准库中没有任何内容可以完全完成 dir() 的功能,但您可以使用 java.lang.reflect 获得相同的信息。具体来说,有关发现类的文档中解释了调查和发现类的成员成员。使用该 API,您可以轻松找到您需要了解的有关类属性的信息。

事实上,自己实现 dir() 只需定义一个方法,该方法可以内省类的方法和字段,然后组装信息集合或打印您想要的任何信息。知道。

dir() 在 Java 中的用处有限,因为 Java 不是交互式的,但如果您需要它用于教育/调查目的或应用程序逻辑,反射 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.

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.

稍尽春風 2024-10-28 01:52:07

我在想使用 javap 是否是一个不错的选择。那人说
javap - Java 类文件反汇编器

来查看内置方法

javap java.lang.Double | grep -i int
public static final int MAX_EXPONENT;
public static final int MIN_EXPONENT;
public static final int SIZE;
public int intValue();
public int hashCode();
public int compareTo(java.lang.Double);
public static int compare(double, double);
public int compareTo(java.lang.Object);

,我可以通过使用 System.out.println()

javap -c java.lang.System | grep -i out
public static final java.io.PrintStream out;
public static void setOut(java.io.PrintStream);
   4: invokestatic  #4                  // Method setOut0:(Ljava/io/PrintStream;)V
   8: putstatic     #98                 // Field out:Ljava/io/PrintStream;

,然后执行 javap java.io。打印流

I was thinking if using javap would be a good choice. The man says
javap - The Java Class File Disassembler

and I could see the built in methods by

javap java.lang.Double | grep -i int
public static final int MAX_EXPONENT;
public static final int MIN_EXPONENT;
public static final int SIZE;
public int intValue();
public int hashCode();
public int compareTo(java.lang.Double);
public static int compare(double, double);
public int compareTo(java.lang.Object);

Taking System.out.println(),

javap -c java.lang.System | grep -i out
public static final java.io.PrintStream out;
public static void setOut(java.io.PrintStream);
   4: invokestatic  #4                  // Method setOut0:(Ljava/io/PrintStream;)V
   8: putstatic     #98                 // Field out:Ljava/io/PrintStream;

and then do javap java.io.PrintStream

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文