当你输入某个对象的名称(myObject。)时,我想编写类似netbeans或eclipse的东西,显示方法和/或属性的列表。我真的不知道如何开始,如果你有任何想法或一些链接来询问它,我将不胜感激。非常感谢你。
答案 0 :(得分:1)
你可以使用 reflection ,比如你得到了班级的名字" Immutable"来自用户,您可以这样做:
String className = "Immutable"; // get it from the user using Scanner
Class c = Class.forName(className);
for (Field f : c.getDeclaredFields()) {
System.out.println("f = " + f);
}
for (Method m : c.getDeclaredMethods()) {
System.out.println("m = " + m);
}
答案 1 :(得分:1)
以下是将反射应用于对象的简单示例:
Object obj = ...;
Class<?> clazz = obj.getClass();
for( Field f: clazz.getDeclaredFields() ){
System.out.println( "Field " + f.getName() + " is a " + f.getType() );
}