将类型参数传递给接口的方法
有以下类:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您似乎要走很长的路才能在字段上调用 setter 函数;-)。当
newValue
是String
时,您希望知道setValue
调用中的new Setter()
是怎样的> 什么时候是别的东西?有了setter的类型参数,我想应该不难:
You seem to be going a rather long way to call a setter function on a field ;-). How is the
new Setter()
in thesetValue
call you want to have supposed to know whennewValue
is aString
and when it is something else?With a type parameter for setter, I think it should not be difficult:
这是您要找的吗?
Is this what you are looking for?