日期:2014-05-20 浏览次数:20720 次
class S {
//这里面怎么写getFieldNames()
}
class A extends S {
//这里要重写getFieldNames(),怎么实现
}
class G<T extends S> {
void fun(){
//调用getFieldNames()做一些事件.
T.getFieldNames();
}
}
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
class S {
public static String[] getFieldNames(){
return new String[]{"a","b"};
}
}
class A extends S {
public static String[] getFieldNames(){
return new String[]{"c","d"};
}
}
class G<T extends S> {
void fun(Class<T> t){
//调用getFieldNames()做一些事件.
try {
System.out.println(Arrays.toString((Object[]) t.getDeclaredMethod("getFieldNames").invoke(null)));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
}
public class TestStatic {
/**
* @param args
*/
public static void main(String[] args) {
G<S> g=new G<S>();
g.fun(S.class);
G<A> g2=new G<A>();
g2.fun(A.class);
}
}