先生成一个FieldCommon的实例,然后定义你的method指向sex. FieldCommon fieldCommon = new FieldCommon(); String method = fieldCommon.SEX;
我建议直接写:String method=FieldCommon.SEX;
To: cxy550(小宇): 根本不需要先生成一个FieldCommon的实例,你没见那些数据都是static 的吗?方法一:method是FieldCommon类的成员函数,那么:
public String getSex() { return SEX; } method是其他类类的成员函数,那么: public String getSex() { return FieldCommon.SEX; }方法二: String method=FieldCommon.SEX;static 数据的调用方法:类名.数据成员 !static方法也一样!
大家似乎理解错我的意思了.不过我找到解决的办法了. public static Object getCommonValue(String classname,String fieldname){ try{ Class c = Class.forName(classname); Field field = c.getField(fieldname); return field.get(fieldname); }catch(Exception e){e.printStackTrace();} return null; } FieldClass Item=(FieldClass)getCommonValue("FieldCommon",fieldname);
大家似乎理解错我的意思了.不过我找到解决的办法了. public static Object getCommonValue(String classname,String fieldname){ try{ Class c = Class.forName(classname); Field field = c.getField(fieldname); return field.get(fieldname); }catch(Exception e){e.printStackTrace();} return null; } FieldClass Item=(FieldClass)getCommonValue("FieldCommon",fieldname);
FieldCommon fieldCommon = new FieldCommon();
String method = fieldCommon.SEX;
public String getSex()
{
return SEX;
}
method是其他类类的成员函数,那么:
public String getSex()
{
return FieldCommon.SEX;
}方法二:
String method=FieldCommon.SEX;static 数据的调用方法:类名.数据成员 !static方法也一样!
public static Object getCommonValue(String classname,String fieldname){
try{
Class c = Class.forName(classname);
Field field = c.getField(fieldname);
return field.get(fieldname);
}catch(Exception e){e.printStackTrace();}
return null;
} FieldClass Item=(FieldClass)getCommonValue("FieldCommon",fieldname);
public static Object getCommonValue(String classname,String fieldname){
try{
Class c = Class.forName(classname);
Field field = c.getField(fieldname);
return field.get(fieldname);
}catch(Exception e){e.printStackTrace();}
return null;
} FieldClass Item=(FieldClass)getCommonValue("FieldCommon",fieldname);