import java.lang.reflect.Field;
public class Main {
public static void main(String args[]) {
System.out.println(new MyClass().toString());
}
}
class MyClass {
String hello = "hi";
int i = 0;
public String toString() {
StringBuilder sb = new StringBuilder();
Class cls = getClass();
Field[] f = cls.getDeclaredFields();
for (int i = 0; i < f.length; i++) {
f[i].setAccessible(true);
try {
sb.append(f[i].getName()+"="+ f[i].get(this)+"\n");
} catch (Exception e) {
e.printStackTrace();
}
}
if (cls.getSuperclass().getSuperclass() != null) {
sb.append("super:"+ super.toString()+"\n");
}
return cls.getName()+"\n" + sb.toString();
}
}
|