import java.lang.reflect.Field
public class Test3 {
public static void main(String[] args) throws Exception {
T t = new T();
t.getS1Name();
t.getS2Name();
}
}
class T{
String s1;
String s2;
public T() {
}
void getS1Name() throws Exception{
method(s1);
}
void getS2Name() throws Exception{
method(s2);
}
//获取传入参数的变量名,并打印出来
void method(String s) throws Exception, Exception{
Field[] fields = s.getClass().getFields();
for(Field field : fields){
if(field.getType() == String.class){
String oldValue = (String)field.get(s);
System.out.println(oldValue);
}
}
}
} |