具有不同签名但相同主体的功能

Functions with different signatures, but the same body

考虑 class

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() 函数具有不同的签名,但主体相同。有没有一种漂亮的方法可以缩短这段代码,以免在四个不同的函数中复制粘贴相同的代码?

您可以创建泛型方法,如下所示:

 public<T> void addShortcuts(T[] keyStrokes) {
     for (T keyStroke : keyStrokes) {
         addShortcut(keyStroke);
     }
 }

如果您重新排序参数并使用可变参数,您可以将它们缩减为两个构造函数:

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

除了其他答案之外,您还可以使用一个技巧,通过添加私有构造函数至少将 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);
    }
    ...
}