将类型参数传递给接口方法

时间:2012-02-15 19:34:50

标签: java generics interface anonymous-class

有以下课程:

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以使其正常工作(如果可能的话)?

2 个答案:

答案 0 :(得分:1)

你似乎要在一个字段上调用setter函数的方式很长;-)。如果new Setter()setValue,那么newValue电话中String的{​​{1}}怎么会知道呢?

使用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
    }    
}   

答案 1 :(得分:0)

这是你在找什么?

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