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

发布于 2025-01-05 18:36:09 字数 1367 浏览 0 评论 0原文

有以下类:

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

There's the following 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
    }       
}

Then there's PersonWriter, which extends AbstractWriter and looks currently like this:

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

But I want setName to look like this:

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

How do I have to modify AbstractWriter, to make it work (if it's even possible)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

病毒体 2025-01-12 18:36:09

您似乎要走很长的路才能在字段上调用 ​​setter 函数;-)。当 newValueString 时,您希望知道 setValue 调用中的 new Setter() 是怎样的> 什么时候是别的东西?

有了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
    }    
}   

You seem to be going a rather long way to call a setter function on a field ;-). How is the new Setter() in the setValue call you want to have supposed to know when newValue is a String and when it is something else?

With a type parameter for setter, I think it should not be difficult:

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
    }    
}   
很酷不放纵 2025-01-12 18:36:09

这是您要找的吗?

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

Is this what you are looking for?

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);
            }
        });
    };
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文