import java.lang.reflect.*;class Ref{ public static final String XYZ="I am XYZ"; public static void main(String[] args) { try{ Field f = Class.forName("Ref").getField("XYZ"); String s = (String)f.get(Class.forName("Ref").newInstance()); System.out.println(s); }catch(Exception e){e.printStackTrace();} } }
package myPackage; public class JRXlsExporterParameter { protected JRXlsExporterParameter(String name){ //super(name); } public String toString(){ return "fog628"; } public static final JRXlsExporterParameter IS_ONE_PAGE_PER_SHEET = new JRXlsExporterParameter("Is One Page per Sheet"); public static final JRXlsExporterParameter IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS = new JRXlsExporterParameter("Is Remove Empty Space Between Rows"); public static final JRXlsExporterParameter IS_WHITE_PAGE_BACKGROUND = new JRXlsExporterParameter("Is White Page Background"); public static final JRXlsExporterParameter IS_AUTO_DETECT_CELL_TYPE = new JRXlsExporterParameter("Is Auto Detect Cell Type"); } 这是楼主的原类:改了一点点东西 1、package myPackage; 为了说明构造函数是protected 2、没有extends JRExporterParameter 3、加了一个toString()用来打印======================================================= import java.lang.reflect.*; import myPackage.JRXlsExporterParameter;public class Ref{ public static void main(String[] args) throws Exception{ Class c = JRXlsExporterParameter.class; Field f = c.getDeclaredField("IS_ONE_PAGE_PER_SHEET"); //调用静态的东西不用生成一个新的实例 Object o = f.get(null); System.out.println(o);//结果为fog628 } }
public static final String XYZ="I am XYZ";
public static void main(String[] args) {
try{
Field f = Class.forName("Ref").getField("XYZ");
String s = (String)f.get(Class.forName("Ref").newInstance());
System.out.println(s);
}catch(Exception e){e.printStackTrace();}
}
}
public class JRXlsExporterParameter
{
protected JRXlsExporterParameter(String name){
//super(name);
}
public String toString(){
return "fog628";
}
public static final JRXlsExporterParameter IS_ONE_PAGE_PER_SHEET = new JRXlsExporterParameter("Is One Page per Sheet");
public static final JRXlsExporterParameter IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS = new JRXlsExporterParameter("Is Remove Empty Space Between Rows");
public static final JRXlsExporterParameter IS_WHITE_PAGE_BACKGROUND = new JRXlsExporterParameter("Is White Page Background");
public static final JRXlsExporterParameter IS_AUTO_DETECT_CELL_TYPE = new JRXlsExporterParameter("Is Auto Detect Cell Type");
}
这是楼主的原类:改了一点点东西
1、package myPackage; 为了说明构造函数是protected
2、没有extends JRExporterParameter
3、加了一个toString()用来打印=======================================================
import java.lang.reflect.*;
import myPackage.JRXlsExporterParameter;public class Ref{
public static void main(String[] args) throws Exception{
Class c = JRXlsExporterParameter.class;
Field f = c.getDeclaredField("IS_ONE_PAGE_PER_SHEET");
//调用静态的东西不用生成一个新的实例
Object o = f.get(null);
System.out.println(o);//结果为fog628
}
}