gpt4 book ai didi

java - 动态找到正确的方法以避免重复相同的代码

转载 作者:行者123 更新时间:2023-11-29 08:22:44 25 4
gpt4 key购买 nike

<分区>

以下代码将监听器添加到几个 SWT 文本元素。唯一的区别是 Listeners 方法中的代码。有没有办法通过找到正确的动态使用方法来减少这段代码的重复性?

在此示例中使用了 FocusListener,但它不相关。

private void addFocusLostListeners() {
nameText.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {}
@Override
public void focusLost(FocusEvent e) {
myDataObject.setName(nameText.getText());
}
});
ageText.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {}
@Override
public void focusLost(FocusEvent e) {
myDataObject.setAge(ageText.getText());
}
});
emailText.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {}
@Override
public void focusLost(FocusEvent e) {
myDataObject.setEmail(emailText.getText());
}
});
...
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com