有用记得加分。import java.lang.reflect.*; public class testtest{ public String strTest1 = "test"; public String strTest2 = "strTest1";
public String getTest2(){ return strTest2; } public static void main(String[] a){ testtest t = new testtest(); try{ Field s = (Class.forName("testtest")).getField(t.getTest2()); System.out.println( (String)s.get(t) ); }catch(Exception e){ System.out.println("error:"+e); } } }
public class testtest{
public String strTest1 = "test";
public String strTest2 = "strTest1";
public String getTest2(){
return strTest2;
} public static void main(String[] a){
testtest t = new testtest();
try{
Field s = (Class.forName("testtest")).getField(t.getTest2());
System.out.println( (String)s.get(t) );
}catch(Exception e){
System.out.println("error:"+e);
}
}
}
String strTest2 = "strTest1";strTest1 strTest2 本来就是对象,上面两句和
String strTest1 = new String("test");
String strTest2 = new String("strTest1)";
没有什么区别的!