import java.lang.reflect.*;
public class Test {
public static void main(String[] args) throws IllegalAccessException {
byte c[]=null;
C a=new C();
/*try {
a.show();
}catch(IllegalAccessException iae) {
iae.printStackTrace();
}*/
}
}
class C extends A{
public C()throws IllegalAccessException {
super();
}
byte[] d ={0,0,0,0};
}
class A {
int i;
//String b="aaa";
byte c[]={0x23,0x11};
public A() throws IllegalAccessException {
Class me = this.getClass();
Field[] fields = me.getDeclaredFields();
for (Field field : fields)
{
System.out.println("field name is "+field.getName());
System.out.println("field obj is "+ field.get(this));
}
}
}
代码如下
Class me = this.getClass();
Field[] fields = me.getDeclaredFields();// 得到所有成员变量
for (Field field : fields)// 增强for循环
{
System.out.println("field name is "+field.getName()); //打印出field的name
System.out.println("field obj is "+ field.get(this)); //打印出 field 得值
}
import java.lang.reflect.*;
public class Test {
public static void main(String[] args) throws IllegalAccessException {
byte c[]=null;
C a=new C();
/*try {
a.show();
}catch(IllegalAccessException iae) {
iae.printStackTrace();
}*/
}
}
class C extends A{
public C()throws IllegalAccessException {
super();
}
byte[] d ={0,0,0,0};
}
class A {
int i;
//String b="aaa";
byte c[]={0x23,0x11};
public A() throws IllegalAccessException {
Class me = this.getClass();
Field[] fields = me.getDeclaredFields();
for (Field field : fields)
{
System.out.println("field name is "+field.getName());
System.out.println("field obj is "+ field.get(this));
}
}
}
放在非构造器方法中也可以啊,代码如下:
import java.lang.reflect.*;
public class Test {
public static void main(String[] args) throws IllegalAccessException {
A a=new A();
/*try {
a.show();
}catch(IllegalAccessException iae) {
iae.printStackTrace();
}*/
}
}
class A {
int i;
String b;
public A() throws IllegalAccessException {
Class me = this.getClass();
Field[] fields = me.getDeclaredFields();
for (Field field : fields)
{
System.out.println("field name is "+field.getName());
System.out.println("field obj is "+ field.get(this));
}
}
}