本帖最后由 fortitan 于 2013-02-23 09:57:10 编辑

解决方案 »

  1.   

    class 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();
        }
    }
      

  2.   

    变成非static方法的难点在哪里
      

  3.   

    明显这是要做一个策略模式
    先做个接口规范操作。
    A 和 B都实现了这个接口,这样你要用时再决定传入A还是B.静态方法无解。
      

  4.   


    试了下,在fun()中插入T t = new T();,这名话就直接编译不能通过,提示说"Cannot instantiate the type T"
      

  5.   


    我真晕
    S s = new A();或者S s =  new B();
    s.getFieldNames();
      

  6.   

    使用反射可以实现你的要求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);
    }}
      

  7.   

    在网上查了下策略模式,给出个小例子吧,让不知道的人也共同学习吧.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;
    }

    void fun() {
    s.getFieldNames();
    }
    }
    // 当你想写new G<A>() 就要写成 new G(new A())