首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >函数具有不同的签名,但具有相同的主体。

函数具有不同的签名,但具有相同的主体。
EN

Stack Overflow用户
提问于 2018-12-13 12:10:57
回答 3查看 89关注 0票数 2

考虑一个类

代码语言:javascript
复制
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()函数具有不同的签名,但具有相同的主体。为了避免在四个不同的函数中复制相同的代码,是否有一种很好的方法使代码更短一些?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-12-13 12:15:46

如果重新排序参数并使用varargs,可以将它们简化为两个构造函数:

代码语言:javascript
复制
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[]的方法,则可以进一步缩短代码:

代码语言:javascript
复制
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));
}
票数 4
EN

Stack Overflow用户

发布于 2018-12-13 12:49:43

除了其他答案之外,还可以通过添加一个私有构造函数,从您的专用构造函数中至少提取addActionListener(actionListener);。从另一个答案来看,这并不能替代varargs的诡计。您可以应用这两种技巧来获得更小的代码。

代码语言:javascript
复制
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);
    }
    ...
}
票数 2
EN

Stack Overflow用户

发布于 2018-12-13 12:15:40

您可以创建通用方法,如下所示:

代码语言:javascript
复制
 public<T> void addShortcuts(T[] keyStrokes) {
     for (T keyStroke : keyStrokes) {
         addShortcut(keyStroke);
     }
 }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53761568

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档