java 反射 与构造器的关系

xoele 2006-12-13 05:47:53
为什么我在构造器里调用 反射, 取得 field 总是为 null?
构造完毕后可以的,
构造时不能反射吗?
类不是已经有了吗?
...全文
427 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
xoele 2006-12-19
  • 打赏
  • 举报
回复
o ,把 field 改成 static就行了 ,哦也
insiku 2006-12-18
  • 打赏
  • 举报
回复
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));
}
}
}

输出结果:
field name is d
field obj is null


=========================================

this.getClass();
这个时候 this是class C的对象 反射出来的 当然只有class c的field
然后在C的super class A的构造函数中打印 这个时候C当然没有初始化完成
回去看看继承初始化的过程就知道为什么了
xoele 2006-12-18
  • 打赏
  • 举报
回复
我迷了路!!!
xoele 2006-12-17
  • 打赏
  • 举报
回复
echoiori()  
你的例子中蕴含了什么深意呢?我看不出跟我的问题有什么关系啊?
xoele 2006-12-15
  • 打赏
  • 举报
回复
没有花,蝶儿不知归途
xoele 2006-12-15
  • 打赏
  • 举报
回复
大家来研讨一下啊
echoiori 2006-12-15
  • 打赏
  • 举报
回复
希望上面的代码可以为各位提供点帮助。
echoiori 2006-12-15
  • 打赏
  • 举报
回复
//这个是孙鑫视频中的一段代码,在jdk1。4下运行没有问题,在jdk1.5下invoke()这个方法
//使用异常。
//在通过 cmd >java ClassTest Point 运行这个类,动态产生一个Point类的对象,
//动态调用这个对象的方法。
import java.lang.reflect.*;
import java.lang.*;//这个的导入和上面的导入无关。
class ClassTest
{
public static void main(String[] args)
{
try
{
Class c = Class.forName(args[0]);
//此处要写入你类的全路径+类名

Constructor[] cons = c.getDeclaredConstructors();
System.out.println(cons[0]);
Class[] parames = cons[0].getParameterTypes();
//返回一个Class数组;作用为获取构建器[0]中的参数,
//在Constructor<T>中。

Object[] paramesValue = new Object[parames.length];
//因为cons[0].newInstance(Object[] o)
//所以声明了paramsValue这个对象数组。创建一个
//parames.length大小的对象数组。

for (int i=0;i<parames.length;i++)
//根据这个构造方法的参数个数。
{if(parames[i].isPrimitive())
//判断如果是基本类型
{
paramesValue[i] = new Integer(i+5);
//将这个对象列表中添加int对象。
System.out.println(paramesValue[i].toString());
}

}

Object o = cons[0].newInstance(paramesValue);
//返回一个对象,通过此方法获取一个[0]号构造器的对象。
Method[] ms = c.getDeclaredMethods();
//获取这个构造器的方法名
System.out.println("MethodName = "+ms[0]);
//ms[0].invoke(o,0);//这个调用在jdk1.4的时候可以,在1.5的就不行了,

}catch(Exception ex)

{ex.printStackTrace();}
}
}


class Point
{ static
{
System.out.println("static");
}
int x,y;

void output()
{
System.out.println("output:-> x="+x+",y="+y);
}
Point(int x,int y)
{
this.x = x;
this.y = y;
}

}
xoele 2006-12-15
  • 打赏
  • 举报
回复
没有你,我不知答案
w1113 2006-12-15
  • 打赏
  • 举报
回复
up
xoele 2006-12-14
  • 打赏
  • 举报
回复
来人啊,探讨一下
xoele 2006-12-14
  • 打赏
  • 举报
回复
代码如下
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 得值
}

如果这段代码放在构造器中,无论field 是否设了初始值,打出来都是 null,
放在非构造器方法中是可以打印出来初始值的.
xoele 2006-12-14
  • 打赏
  • 举报
回复
thank you very very much
ACCPY111 2006-12-14
  • 打赏
  • 举报
回复
现在快下班了,回家帮你再看看!
xoele 2006-12-14
  • 打赏
  • 举报
回复
大哥,你说的是。。。。。
我的代码有一层继承关系,为什么加了这层关系就不行了呢????



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));
}
}
}

输出结果:
field name is d
field obj is null
ACCPY111 2006-12-14
  • 打赏
  • 举报
回复
上面是放在构造器中但是没设初始值,设了后初始值也是可以打印出来的.
ACCPY111 2006-12-14
  • 打赏
  • 举报
回复
放在非构造器方法中也可以啊,代码如下:
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));
}
}
}

fool_leave 2006-12-13
  • 打赏
  • 举报
回复
代码贴出来看看呀
healer_kx 2006-12-13
  • 打赏
  • 举报
回复
应该没有问题和联系的.

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧