public List newInstantGetFieldNames(){ } public List getFieldNames(){ return ... } } class A extends S { //这里要重写getFieldNames(),怎么实现 } class G<T extends S> { void fun(){ //调用getFieldNames()做一些事件. T.newInstantGetFieldNames.getFieldNames(); } }
变成非static方法的难点在哪里
明显这是要做一个策略模式 先做个接口规范操作。 A 和 B都实现了这个接口,这样你要用时再决定传入A还是B.静态方法无解。
试了下,在fun()中插入T t = new T();,这名话就直接编译不能通过,提示说"Cannot instantiate the type T"
我真晕 S s = new A();或者S s = new B(); s.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); }}
在网上查了下策略模式,给出个小例子吧,让不知道的人也共同学习吧.interface S { String[] getFieldNames(); } class A implements S { public String[] getFieldNames() { return new String[] { "a", "a" }; } } class B implements S { public String[] getFieldNames() { return new String[] { "b", "b" }; } } class G { private S s; public G(S s) { this.s = s; }
//这里面怎么写getFieldNames()
public List newInstantGetFieldNames(){ }
public List getFieldNames(){
return ...
}
}
class A extends S {
//这里要重写getFieldNames(),怎么实现
}
class G<T extends S> {
void fun(){
//调用getFieldNames()做一些事件.
T.newInstantGetFieldNames.getFieldNames();
}
}
先做个接口规范操作。
A 和 B都实现了这个接口,这样你要用时再决定传入A还是B.静态方法无解。
试了下,在fun()中插入T t = new T();,这名话就直接编译不能通过,提示说"Cannot instantiate the type T"
我真晕
S s = new A();或者S s = new B();
s.getFieldNames();
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);
}}
String[] getFieldNames();
}
class A implements S {
public String[] getFieldNames() {
return new String[] { "a", "a" };
}
}
class B implements S {
public String[] getFieldNames() {
return new String[] { "b", "b" };
}
}
class G {
private S s; public G(S s) {
this.s = s;
}
void fun() {
s.getFieldNames();
}
}
// 当你想写new G<A>() 就要写成 new G(new A())