考虑一个类
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ShortcutButton extends JButton {
public ShortcutButton(String text, KeyStroke[] keyStrokes, ActionListener actionListener) {
super(text);
addActionListener(actionListener);
addShortcut(keyStrokes);
}
public ShortcutButton(String text, KeyStroke keyStrokes, ActionListener actionListener) {
super(text);
addActionListener(actionListener);
addShortcut(keyStrokes);
}
public ShortcutButton(String text, String[] keyStrokes, ActionListener actionListener) {
super(text);
addActionListener(actionListener);
addShortcut(keyStrokes);
}
public ShortcutButton(String text, String keyStrokes, ActionListener actionListener) {
super(text);
addActionListener(actionListener);
addShortcut(keyStrokes);
}
public void addShortcuts(KeyStroke[] keyStrokes) {
for (KeyStroke keyStroke : keyStrokes) {
addShortcut(keyStroke);
}
}
public void addShortcuts(String[] keyStrokes) {
for (String keyStroke : keyStrokes) {
addShortcut(keyStroke);
}
}
public void addShortcut(String keyStroke) {
addShortcut(KeyStroke.getKeyStroke(keyStroke));
}
public void addShortcut(KeyStroke keyStroke) {
//some code here
}
}如您所见,ShortcutButton()共构函数和addShortcuts()函数具有不同的签名,但具有相同的主体。为了避免在四个不同的函数中复制相同的代码,是否有一种很好的方法使代码更短一些?
发布于 2018-12-13 12:15:46
如果重新排序参数并使用varargs,可以将它们简化为两个构造函数:
public ShortcutButton(String text, ActionListener actionListener, KeyStroke... keyStrokes) {
super(text);
addActionListener(actionListener);
addShortcuts(keyStrokes);
}
public ShortcutButton(String text, ActionListener actionListener, String... keyStrokes) {
super(text);
addActionListener(actionListener);
addShortcuts(keyStrokes);
}如果有一个将String[]转换为KeyStroke[]的方法,则可以进一步缩短代码:
public ShortcutButton(String text, ActionListener actionListener, KeyStroke... keyStrokes) {
super(text);
addActionListener(actionListener);
addShortcuts(keyStrokes);
}
public ShortcutButton(String text, ActionListener actionListener, String... keyStrokes) {
this(text,actionListener,getShortCuts(keyStrokes));
}发布于 2018-12-13 12:49:43
除了其他答案之外,还可以通过添加一个私有构造函数,从您的专用构造函数中至少提取addActionListener(actionListener);。从另一个答案来看,这并不能替代varargs的诡计。您可以应用这两种技巧来获得更小的代码。
public class ShortcutButton extends JButton {
/** Construct a ShortcutButton without keystrokes. Any constructor calling this should add keystrokes themselves. */
private ShortcutButton(String text, ActionListener actionListener) {
super(text);
addActionListener(actionListener);
}
public ShortcutButton(String text, KeyStroke[] keyStrokes, ActionListener actionListener) {
this(text, actionListener);
addShortcut(keyStrokes);
}
public ShortcutButton(String text, KeyStroke keyStrokes, ActionListener actionListener) {
this(text, actionListener);
addShortcut(keyStrokes);
}
public ShortcutButton(String text, String[] keyStrokes, ActionListener actionListener) {
this(text, actionListener);
addShortcut(keyStrokes);
}
public ShortcutButton(String text, String keyStrokes, ActionListener actionListener) {
this(text, actionListener);
addShortcut(keyStrokes);
}
...
}发布于 2018-12-13 12:15:40
您可以创建通用方法,如下所示:
public<T> void addShortcuts(T[] keyStrokes) {
for (T keyStroke : keyStrokes) {
addShortcut(keyStroke);
}
}https://stackoverflow.com/questions/53761568
复制相似问题