Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 具有不同签名但具有相同主体的函数_Java_Overloading_Constructor Overloading - Fatal编程技术网

Java 具有不同签名但具有相同主体的函数

Java 具有不同签名但具有相同主体的函数,java,overloading,constructor-overloading,Java,Overloading,Constructor Overloading,以班级为例 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) {

以班级为例

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()
co构造函数和
addShortcuts()
函数具有不同的签名,但主体相同。有没有一种很好的方法可以使代码更短,从而避免在四个不同的函数中复制粘贴相同的代码?

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

 public<T> void addShortcuts(T[] keyStrokes) {
     for (T keyStroke : keyStrokes) {
         addShortcut(keyStroke);
     }
 }
public void addShortcuts(T[]击键){
对于(T击键:击键){
添加快捷键(击键);
}
}

如果对参数重新排序并使用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);
}
如果有一种方法可以将
字符串[]
转换为
击键[]
,则可以进一步缩短代码:

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

新建快捷按钮(文本、123、actionListener)->fail@MarkJeronimus谢谢你指出这一点。我把构造函数误认为方法。提问者可以使用另一个答案中提到的方法。上面的方法可以用于addShortcut方法。也许你能做的最重要的事情就是删除代码。反复使用“有用的”重载是没有帮助的。(也可以是“更喜欢组合而不是继承”(或者静态方法而不是继承),但这在GUI编程中似乎是一个失败的原因。)注意那些空数组!我建议检查长度是否为
=1
@MarkJeronimus good point,尽管这可能是
addShortcuts(击键)
的责任(该方法也应该检查OP原始代码中的空数组)。