【问题标题】:Pass type parameter to method of interface将类型参数传递给接口的方法
【发布时间】:2012-02-15 19:34:49
【问题描述】:

有以下类:

public abstract class AbstractWriter<T extends BE> {

    protected final T be;
    // Constructor, some methods

    public static interface Setter {
       void setNewValue();
    }

    protected <S> void setValue(final Class<S> clazz, final S oldValue,
        final S newValue, final Setter setter) {
        // Do something
        setter.setNewValue();
        // Do something
    }       
}

然后是PersonWriter,它扩展了AbstractWriter,目前看起来像这样:

public class PersonWriter extends AbstractWriter<BEPerson> {

    public PersonWriter(BEPerson be) {
        super(be);
    }

    public void setName(String oldValue, final String newValue) {
        setValue(String.class, oldValue, newValue, new Setter() {
            @Override
            public void setNewValue() {
                be.setName(newValue);
            }
        });
    };
}

但我希望setName 看起来像这样:

    public void setName(String oldValue, String newValue) {
        setValue(String.class, oldValue, newValue, new Setter() {
            @Override
            public void setNewValue(String newValue) {
                be.setName(newValue);
            }
        });
    };

我必须如何修改AbstractWriter 才能使其正常工作(如果可能的话)?

【问题讨论】:

    标签: java generics interface anonymous-class


    【解决方案1】:

    在字段上调用 ​​setter 函数似乎还有很长的路要走;-)。你想知道newValue 何时是String 以及何时是其他东西?

    有了setter的类型参数,我觉得应该不难:

    public abstract class AbstractWriter<T extends BE> {
        //...
        public static interface Setter<S> {
            void setNewValue(S newValue);
        }
    
        protected <S> void setValue(final Class<S> clazz, final S oldValue,
            final S newValue, final Setter<S> setter) {
            // Do something
            setter.setNewValue(newValue);
            // Do something
        }    
    }   
    

    【讨论】:

    • 我一开始用setNewValue方法的类型参数试过了,但是没用。
    【解决方案2】:

    这是你要找的吗?

    class BE {
    }
    
    class BEPerson extends BE {
        void setName(String s) {}
        void setAge(Integer i) {}
    }
    
    public abstract class AbstractWriter<T extends BE> {
    
        protected final T be;
        // Constructor, some methods
        AbstractWriter(T be) { this.be = be; }
    
        public static interface Setter<S> {
           void setNewValue(S v);
        }
    
        protected <S> void setValue(final Class<S> clazz, final S oldValue,
            final S newValue, final Setter setter) {
            // Do something
            setter.setNewValue(newValue);
            // Do something
        }       
    }
    
    class PersonWriter extends AbstractWriter<BEPerson> {
    
        public PersonWriter(BEPerson be) {
            super(be);
        }
    
        public void setName(String oldValue, final String newValue) {
            setValue(String.class, oldValue, newValue, new Setter<String>() {
                @Override
                public void setNewValue(String newValue) {
                    be.setName(newValue);
                }
            });
        };
    
        public void setAge(Integer oldValue, final Integer newValue) {
            setValue(Integer.class, oldValue, newValue, new Setter<Integer>() {
                @Override
                public void setNewValue(Integer newValue) {
                    be.setAge(newValue);
                }
            });
        };
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-26
      • 2016-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-07
      • 1970-01-01
      • 2010-11-15
      相关资源
      最近更新 更多