Java 8 Lambda表达式-嵌套类中的多个方法呢?


84

我在以下位置阅读有关新功能的信息:http : //www.javaworld.com/article/2078836/java-se/love-and-hate-for-java-8.html

我看到了下面的示例:

使用匿名类:

button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
        System.out.println("Action Detected");
    }
});

使用Lambda:

button.addActionListener(e -> {
    System.out.println("Action Detected");
});

如果某人MouseListener想要在匿名类中实现多个方法,该怎么办,例如:

public void mousePressed(MouseEvent e) {
    saySomething("Mouse pressed; # of clicks: "
               + e.getClickCount(), e);
}

public void mouseReleased(MouseEvent e) {
    saySomething("Mouse released; # of clicks: "
               + e.getClickCount(), e);
}

... 等等?


21
然后,您只需要使用匿名类。lambda的引入并不会使它们过时。Lambda只能与功能接口一起使用。
Rohit Jain

1
使用lambda作为ActionListener方法非常奇怪。您怎么知道您正在实现actionPerformed接口方法ActionListener
bobbel 2014年

3
@bobbel这是功能接口的整个概念,我建议您先对它们进行阅读,然后再评论它们。
skiwi 2014年

我知道,它将如何运作!关键是,这不是可读性很强的代码,对吗?只是想提一下...
bobbel 2014年

3
@ maythesource.com您可以创建两个附加接口,例如MousePressedListenerMouseReleasedListener和两个方法addMousePressedListener(Button, MousePressedListener)addMouseReleasedListener(Button, MouseReleasedListener)。然后,您可以使用lambda来实现这些事件处理程序。
ZhekaKozlov 2014年

Answers:


80

您可以通过使用辅助程序接口将多方法接口与lambda一起使用。这适用于这样的侦听器接口,在这些接口中,不需要的方法的实现是微不足道的(即,我们也可以做MouseAdapter提供的事情):

// note the absence of mouseClicked…
interface ClickedListener extends MouseListener
{
    @Override
    public default void mouseEntered(MouseEvent e) {}

    @Override
    public default void mouseExited(MouseEvent e) {}

    @Override
    public default void mousePressed(MouseEvent e) {}

    @Override
    public default void mouseReleased(MouseEvent e) {}
}

您只需要定义一次这样的帮助程序接口。

现在,您可以Component c像这样添加点击事件的侦听器:

c.addMouseListener((ClickedListener)(e)->System.out.println("Clicked !"));

7
在我看来,这对于MouseListeners而言是一个解决方案。我认为附加匿名对象(旧方法)效果更好。在我看来,这是个矛盾。
ncmathsadist 2014年

2
@ncmathsadist:如果愿意,可以随时使用匿名内部类方法。这只是一个选择。您也可以考虑这个答案。还有另一个动态的监听器生成器……
Holger 2014年

11
我相信这个“​​“脚”应该是公认的答案。OP的问题很明显是“如果某人想要在匿名类中实现多个方法,例如,MouseListener,该怎么办?”(强调我的意思)。当您拥有一个包含多个方法的接口时,他在问如何使用lamda。@Holger完美地回答了这个问题。OP显然已经知道如何以非功能性方式使用匿名类。

@Holger在使用lambda时,有四种方法会被调用?由于所有这些方法都具有相同的参数。
dreamcoder '18

1
@dreamcoder这四个方法都不会由lambda实现,因为它们都不是抽象的。lambda将实现mouseClicked已在MouseListener接口中声明为抽象方法且未被ClickedListener接口覆盖的方法(因此命名)。
Holger

89

JLS 9.8起

功能接口是仅具有一种抽象方法的接口,因此表示单个功能协定。

Lambda需要这些功能接口,因此仅限于它们的单一方法。仍然需要使用匿名接口来实现多方法接口。

addMouseListener(new MouseAdapter() {

    @Override
    public void mouseReleased(MouseEvent e) {
       ...
    }

    @Override
    public void mousePressed(MouseEvent e) {
      ...
    }
});

13
OP中的问题是“如果某人想要在匿名类中实现多个方法,例如,MouseListener会怎么做?”(强调我的意思)。您给出的答案很好,但只是说您不能使用包含多个方法进行lambda描述的接口。Holger的答案说明了一种解决方法。您可以扩展到自己的界面,并为不需要的方法提供默认值。剩下一个留给您的lambda。这并不是人们在Java 8代码中看到的第一个令人毛骨悚然的东西。

34

Lambda EG确实考虑了这个问题。许多库都使用功能接口,即使它们是在功能接口成为现实之前就设计的。但是有时候确实会发生一个类具有多个抽象方法的情况,而您只想用lambda作为目标。

此处官方推荐的模式是定义工厂方法:

static MouseListener clickHandler(Consumer<MouseEvent> c) { return ... }

这些可以由API本身直接完成(这些可以是内的静态方法MouseListener),也可以是其他一些库中的外部帮助器方法(如果维护者选择不提供这种便利)。因为需要这样做的情况很小,而且解决方法非常简单,所以似乎并没有说服进一步扩展语言以挽救这种极端情况。

采用了类似的技巧ThreadLocal; 请参阅新的静态工厂方法withInitial(Supplier<S>)

(顺便说一句,当出现此问题时,示例几乎总是MouseListener,这是令人鼓舞的,因为它表明希望对lambda友好但对lambda友好的类的集合实际上很小。)


3
另请参阅此问题。还有一些其他的多方法接口,例如WindowListener,但是它们相对较少。
斯图尔特(Stuart Marks)2014年

4

JavaActionListener必须仅实现一个单一方法(actionPerformed(ActionEvent e))。这非常适合Java 8函数,因此Java 8提供了一个简单的lambda来实现ActionListener

MouseAdapter至少需要两个方法,所以不适合作为function


1

我创建了适配器类,即使原始侦听器接口包含多个方法,该类也可以编写(单行)lambda函数侦听器。

请注意InsertOrRemoveUpdate适配器,它将2个事件折叠为一个。

我的原始类还包含WindowFocusListener,TableModelListener,TableColumnModelListener和TreeModelListener的实现,但这些代码使SO限制超过30000个字符。

import java.awt.event.*;
import java.beans.*;
import javax.swing.*;
import javax.swing.event.*;
import org.slf4j.*;

public class Adapters {
  private static final Logger LOGGER = LoggerFactory.getLogger(Adapters.class);

  @FunctionalInterface
  public static interface AdapterEventHandler<EVENT> {
    void handle(EVENT e) throws Exception;

    default void handleWithRuntimeException(EVENT e) {
      try {
        handle(e);
      }
      catch(Exception exception) {
        throw exception instanceof RuntimeException ? (RuntimeException) exception : new RuntimeException(exception);
      }
    }
  }

  public static void main(String[] args) throws Exception {
    // -------------------------------------------------------------------------------------------------------------------------------------
    // demo usage
    // -------------------------------------------------------------------------------------------------------------------------------------
    JToggleButton toggleButton = new JToggleButton();
    JFrame frame = new JFrame();
    JTextField textField = new JTextField();
    JScrollBar scrollBar = new JScrollBar();
    ListSelectionModel listSelectionModel = new DefaultListSelectionModel();

    // ActionListener
    toggleButton.addActionListener(ActionPerformed.handle(e -> LOGGER.info(e.toString())));

    // ItemListener
    toggleButton.addItemListener(ItemStateChanged.handle(e -> LOGGER.info(e.toString())));

    // ChangeListener
    toggleButton.addChangeListener(StateChanged.handle(e -> LOGGER.info(e.toString())));

    // MouseListener
    frame.addMouseListener(MouseClicked.handle(e -> LOGGER.info(e.toString())));
    frame.addMouseListener(MousePressed.handle(e -> LOGGER.info(e.toString())));
    frame.addMouseListener(MouseReleased.handle(e -> LOGGER.info(e.toString())));
    frame.addMouseListener(MouseEntered.handle(e -> LOGGER.info(e.toString())));
    frame.addMouseListener(MouseExited.handle(e -> LOGGER.info(e.toString())));

    // MouseMotionListener
    frame.addMouseMotionListener(MouseDragged.handle(e -> LOGGER.info(e.toString())));
    frame.addMouseMotionListener(MouseMoved.handle(e -> LOGGER.info(e.toString())));

    // MouseWheelListenere
    frame.addMouseWheelListener(MouseWheelMoved.handle(e -> LOGGER.info(e.toString())));

    // KeyListener
    frame.addKeyListener(KeyTyped.handle(e -> LOGGER.info(e.toString())));
    frame.addKeyListener(KeyPressed.handle(e -> LOGGER.info(e.toString())));
    frame.addKeyListener(KeyReleased.handle(e -> LOGGER.info(e.toString())));

    // FocusListener
    frame.addFocusListener(FocusGained.handle(e -> LOGGER.info(e.toString())));
    frame.addFocusListener(FocusLost.handle(e -> LOGGER.info(e.toString())));

    // ComponentListener
    frame.addComponentListener(ComponentMoved.handle(e -> LOGGER.info(e.toString())));
    frame.addComponentListener(ComponentResized.handle(e -> LOGGER.info(e.toString())));
    frame.addComponentListener(ComponentShown.handle(e -> LOGGER.info(e.toString())));
    frame.addComponentListener(ComponentHidden.handle(e -> LOGGER.info(e.toString())));

    // ContainerListener
    frame.addContainerListener(ComponentAdded.handle(e -> LOGGER.info(e.toString())));
    frame.addContainerListener(ComponentRemoved.handle(e -> LOGGER.info(e.toString())));

    // HierarchyListener
    frame.addHierarchyListener(HierarchyChanged.handle(e -> LOGGER.info(e.toString())));

    // PropertyChangeListener
    frame.addPropertyChangeListener(PropertyChange.handle(e -> LOGGER.info(e.toString())));
    frame.addPropertyChangeListener("propertyName", PropertyChange.handle(e -> LOGGER.info(e.toString())));

    // WindowListener
    frame.addWindowListener(WindowOpened.handle(e -> LOGGER.info(e.toString())));
    frame.addWindowListener(WindowClosing.handle(e -> LOGGER.info(e.toString())));
    frame.addWindowListener(WindowClosed.handle(e -> LOGGER.info(e.toString())));
    frame.addWindowListener(WindowIconified.handle(e -> LOGGER.info(e.toString())));
    frame.addWindowListener(WindowDeiconified.handle(e -> LOGGER.info(e.toString())));
    frame.addWindowListener(WindowActivated.handle(e -> LOGGER.info(e.toString())));
    frame.addWindowListener(WindowDeactivated.handle(e -> LOGGER.info(e.toString())));

    // WindowStateListener
    frame.addWindowStateListener(WindowStateChanged.handle(e -> LOGGER.info(e.toString())));

    // DocumentListener
    textField.getDocument().addDocumentListener(InsertUpdate.handle(e -> LOGGER.info(e.toString())));
    textField.getDocument().addDocumentListener(RemoveUpdate.handle(e -> LOGGER.info(e.toString())));
    textField.getDocument().addDocumentListener(InsertOrRemoveUpdate.handle(e -> LOGGER.info(e.toString())));
    textField.getDocument().addDocumentListener(ChangedUpdate.handle(e -> LOGGER.info(e.toString())));

    // AdjustmentListener
    scrollBar.addAdjustmentListener(AdjustmentValueChanged.handle(e -> LOGGER.info(e.toString())));

    // ListSelectionListener
    listSelectionModel.addListSelectionListener(ValueChanged.handle(e -> LOGGER.info(e.toString())));

    // ...
    // enhance as needed
  }

  // @formatter:off
  // ---------------------------------------------------------------------------------------------------------------------------------------
  // ActionListener
  // ---------------------------------------------------------------------------------------------------------------------------------------
  public static class ActionPerformed implements ActionListener {
    private AdapterEventHandler<ActionEvent> m_handler = null;
    public static ActionPerformed handle(AdapterEventHandler<ActionEvent> handler) { ActionPerformed adapter = new ActionPerformed(); adapter.m_handler = handler; return adapter; }
    @Override public void actionPerformed(ActionEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  // ---------------------------------------------------------------------------------------------------------------------------------------
  // ItemListener
  // ---------------------------------------------------------------------------------------------------------------------------------------
  public static class ItemStateChanged implements ItemListener {
    private AdapterEventHandler<ItemEvent> m_handler = null;
    public static ItemStateChanged handle(AdapterEventHandler<ItemEvent> handler) { ItemStateChanged adapter = new ItemStateChanged(); adapter.m_handler = handler; return adapter; }
    @Override public void itemStateChanged(ItemEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  // ---------------------------------------------------------------------------------------------------------------------------------------
  // ChangeListener
  // ---------------------------------------------------------------------------------------------------------------------------------------
  public static class StateChanged implements ChangeListener {
    private AdapterEventHandler<ChangeEvent> m_handler = null;
    public static StateChanged handle(AdapterEventHandler<ChangeEvent> handler) { StateChanged adapter = new StateChanged(); adapter.m_handler = handler; return adapter; }
    @Override public void stateChanged(ChangeEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  // ---------------------------------------------------------------------------------------------------------------------------------------
  // MouseListener
  // ---------------------------------------------------------------------------------------------------------------------------------------
  public static class MouseClicked extends MouseAdapter {
    private AdapterEventHandler<MouseEvent> m_handler = null;
    public static MouseClicked handle(AdapterEventHandler<MouseEvent> handler) { MouseClicked adapter = new MouseClicked(); adapter.m_handler = handler; return adapter; }
    @Override public void mouseClicked(MouseEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class MousePressed extends MouseAdapter {
    private AdapterEventHandler<MouseEvent> m_handler = null;
    public static MousePressed handle(AdapterEventHandler<MouseEvent> handler) { MousePressed adapter = new MousePressed(); adapter.m_handler = handler; return adapter; }
    @Override public void mousePressed(MouseEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class MouseReleased extends MouseAdapter {
    private AdapterEventHandler<MouseEvent> m_handler = null;
    public static MouseReleased handle(AdapterEventHandler<MouseEvent> handler) { MouseReleased adapter = new MouseReleased(); adapter.m_handler = handler; return adapter; }
    @Override public void mouseReleased(MouseEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class MouseEntered extends MouseAdapter {
    private AdapterEventHandler<MouseEvent> m_handler = null;
    public static MouseEntered handle(AdapterEventHandler<MouseEvent> handler) { MouseEntered adapter = new MouseEntered(); adapter.m_handler = handler; return adapter; }
    @Override public void mouseEntered(MouseEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class MouseExited extends MouseAdapter {
    private AdapterEventHandler<MouseEvent> m_handler = null;
    public static MouseExited handle(AdapterEventHandler<MouseEvent> handler) { MouseExited adapter = new MouseExited(); adapter.m_handler = handler; return adapter; }
    @Override public void mouseExited(MouseEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  // ---------------------------------------------------------------------------------------------------------------------------------------
  // MouseMotionListener
  // ---------------------------------------------------------------------------------------------------------------------------------------
  public static class MouseDragged extends MouseAdapter {
    private AdapterEventHandler<MouseEvent> m_handler = null;
    public static MouseDragged handle(AdapterEventHandler<MouseEvent> handler) { MouseDragged adapter = new MouseDragged(); adapter.m_handler = handler; return adapter; }
    @Override public void mouseDragged(MouseEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class MouseMoved extends MouseAdapter {
    private AdapterEventHandler<MouseEvent> m_handler = null;
    public static MouseMoved handle(AdapterEventHandler<MouseEvent> handler) { MouseMoved adapter = new MouseMoved(); adapter.m_handler = handler; return adapter; }
    @Override public void mouseMoved(MouseEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  // ---------------------------------------------------------------------------------------------------------------------------------------
  // MouseWheelListener
  // ---------------------------------------------------------------------------------------------------------------------------------------
  public static class MouseWheelMoved extends MouseAdapter {
    private AdapterEventHandler<MouseWheelEvent> m_handler = null;
    public static MouseWheelMoved handle(AdapterEventHandler<MouseWheelEvent> handler) { MouseWheelMoved adapter = new MouseWheelMoved(); adapter.m_handler = handler; return adapter; }
    @Override public void mouseWheelMoved(MouseWheelEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  // ---------------------------------------------------------------------------------------------------------------------------------------
  // KeyListener
  // ---------------------------------------------------------------------------------------------------------------------------------------
  public static class KeyTyped extends KeyAdapter {
    private AdapterEventHandler<KeyEvent> m_handler = null;
    public static KeyTyped handle(AdapterEventHandler<KeyEvent> handler) { KeyTyped adapter = new KeyTyped(); adapter.m_handler = handler; return adapter; }
    @Override public void keyTyped(KeyEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class KeyPressed extends KeyAdapter {
    private AdapterEventHandler<KeyEvent> m_handler = null;
    public static KeyPressed handle(AdapterEventHandler<KeyEvent> handler) { KeyPressed adapter = new KeyPressed(); adapter.m_handler = handler; return adapter; }
    @Override public void keyPressed(KeyEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class KeyReleased extends KeyAdapter {
    private AdapterEventHandler<KeyEvent> m_handler = null;
    public static KeyReleased handle(AdapterEventHandler<KeyEvent> handler) { KeyReleased adapter = new KeyReleased(); adapter.m_handler = handler; return adapter; }
    @Override public void keyReleased(KeyEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  // ---------------------------------------------------------------------------------------------------------------------------------------
  // FocusListener
  // ---------------------------------------------------------------------------------------------------------------------------------------
  public static class FocusGained extends FocusAdapter {
    private AdapterEventHandler<FocusEvent> m_handler = null;
    public static FocusGained handle(AdapterEventHandler<FocusEvent> handler) { FocusGained adapter = new FocusGained(); adapter.m_handler = handler; return adapter; }
    @Override public void focusGained(FocusEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class FocusLost extends FocusAdapter {
    private AdapterEventHandler<FocusEvent> m_handler = null;
    public static FocusLost handle(AdapterEventHandler<FocusEvent> handler) { FocusLost adapter = new FocusLost(); adapter.m_handler = handler; return adapter; }
    @Override public void focusLost(FocusEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  // ---------------------------------------------------------------------------------------------------------------------------------------
  // ComponentListener
  // ---------------------------------------------------------------------------------------------------------------------------------------
  public static class ComponentMoved extends ComponentAdapter {
    private AdapterEventHandler<ComponentEvent> m_handler = null;
    public static ComponentMoved handle(AdapterEventHandler<ComponentEvent> handler) { ComponentMoved adapter = new ComponentMoved(); adapter.m_handler = handler; return adapter; }
    @Override public void componentMoved(ComponentEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class ComponentResized extends ComponentAdapter {
    private AdapterEventHandler<ComponentEvent> m_handler = null;
    public static ComponentResized handle(AdapterEventHandler<ComponentEvent> handler) { ComponentResized adapter = new ComponentResized(); adapter.m_handler = handler; return adapter; }
    @Override public void componentResized(ComponentEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class ComponentShown extends ComponentAdapter {
    private AdapterEventHandler<ComponentEvent> m_handler = null;
    public static ComponentShown handle(AdapterEventHandler<ComponentEvent> handler) { ComponentShown adapter = new ComponentShown(); adapter.m_handler = handler; return adapter; }
    @Override public void componentShown(ComponentEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class ComponentHidden extends ComponentAdapter {
    private AdapterEventHandler<ComponentEvent> m_handler = null;
    public static ComponentHidden handle(AdapterEventHandler<ComponentEvent> handler) { ComponentHidden adapter = new ComponentHidden(); adapter.m_handler = handler; return adapter; }
    @Override public void componentHidden(ComponentEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  // ---------------------------------------------------------------------------------------------------------------------------------------
  // ContainerListener
  // ---------------------------------------------------------------------------------------------------------------------------------------
  public static class ComponentAdded extends ContainerAdapter {
    private AdapterEventHandler<ContainerEvent> m_handler = null;
    public static ComponentAdded handle(AdapterEventHandler<ContainerEvent> handler) { ComponentAdded adapter = new ComponentAdded(); adapter.m_handler = handler; return adapter; }
    @Override public void componentAdded(ContainerEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class ComponentRemoved extends ContainerAdapter {
    private AdapterEventHandler<ContainerEvent> m_handler = null;
    public static ComponentRemoved handle(AdapterEventHandler<ContainerEvent> handler) { ComponentRemoved adapter = new ComponentRemoved(); adapter.m_handler = handler; return adapter; }
    @Override public void componentRemoved(ContainerEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  // ---------------------------------------------------------------------------------------------------------------------------------------
  // HierarchyListener
  // ---------------------------------------------------------------------------------------------------------------------------------------
  public static class HierarchyChanged implements HierarchyListener {
    private AdapterEventHandler<HierarchyEvent> m_handler = null;
    public static HierarchyChanged handle(AdapterEventHandler<HierarchyEvent> handler) { HierarchyChanged adapter = new HierarchyChanged(); adapter.m_handler = handler; return adapter; }
    @Override public void hierarchyChanged(HierarchyEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  // ---------------------------------------------------------------------------------------------------------------------------------------
  // PropertyChangeListener
  // ---------------------------------------------------------------------------------------------------------------------------------------
  public static class PropertyChange implements PropertyChangeListener {
    private AdapterEventHandler<PropertyChangeEvent> m_handler = null;
    public static PropertyChange handle(AdapterEventHandler<PropertyChangeEvent> handler) { PropertyChange adapter = new PropertyChange(); adapter.m_handler = handler; return adapter; }
    @Override public void propertyChange(PropertyChangeEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  // ---------------------------------------------------------------------------------------------------------------------------------------
  // WindowListener
  // ---------------------------------------------------------------------------------------------------------------------------------------
  public static class WindowOpened extends WindowAdapter {
    private AdapterEventHandler<WindowEvent> m_handler = null;
    public static WindowOpened handle(AdapterEventHandler<WindowEvent> handler) { WindowOpened adapter = new WindowOpened(); adapter.m_handler = handler; return adapter; }
    @Override public void windowOpened(WindowEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class WindowClosing extends WindowAdapter {
    private AdapterEventHandler<WindowEvent> m_handler = null;
    public static WindowClosing handle(AdapterEventHandler<WindowEvent> handler) { WindowClosing adapter = new WindowClosing(); adapter.m_handler = handler; return adapter; }
    @Override public void windowClosing(WindowEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class WindowClosed extends WindowAdapter {
    private AdapterEventHandler<WindowEvent> m_handler = null;
    public static WindowClosed handle(AdapterEventHandler<WindowEvent> handler) { WindowClosed adapter = new WindowClosed(); adapter.m_handler = handler; return adapter; }
    @Override public void windowClosed(WindowEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class WindowIconified extends WindowAdapter {
    private AdapterEventHandler<WindowEvent> m_handler = null;
    public static WindowIconified handle(AdapterEventHandler<WindowEvent> handler) { WindowIconified adapter = new WindowIconified(); adapter.m_handler = handler; return adapter; }
    @Override public void windowIconified(WindowEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class WindowDeiconified extends WindowAdapter {
    private AdapterEventHandler<WindowEvent> m_handler = null;
    public static WindowDeiconified handle(AdapterEventHandler<WindowEvent> handler) { WindowDeiconified adapter = new WindowDeiconified(); adapter.m_handler = handler; return adapter; }
    @Override public void windowDeiconified(WindowEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class WindowActivated extends WindowAdapter {
    private AdapterEventHandler<WindowEvent> m_handler = null;
    public static WindowActivated handle(AdapterEventHandler<WindowEvent> handler) { WindowActivated adapter = new WindowActivated(); adapter.m_handler = handler; return adapter; }
    @Override public void windowActivated(WindowEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class WindowDeactivated extends WindowAdapter {
    private AdapterEventHandler<WindowEvent> m_handler = null;
    public static WindowDeactivated handle(AdapterEventHandler<WindowEvent> handler) { WindowDeactivated adapter = new WindowDeactivated(); adapter.m_handler = handler; return adapter; }
    @Override public void windowDeactivated(WindowEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  // ---------------------------------------------------------------------------------------------------------------------------------------
  // WindowStateListener
  // ---------------------------------------------------------------------------------------------------------------------------------------
  public static class WindowStateChanged extends WindowAdapter {
    private AdapterEventHandler<WindowEvent> m_handler = null;
    public static WindowStateChanged handle(AdapterEventHandler<WindowEvent> handler) { WindowStateChanged adapter = new WindowStateChanged(); adapter.m_handler = handler; return adapter; }
    @Override public void windowStateChanged(WindowEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  // ---------------------------------------------------------------------------------------------------------------------------------------
  // DocumentListener
  // ---------------------------------------------------------------------------------------------------------------------------------------
  public static class DocumentAdapter implements DocumentListener {
    @Override public void insertUpdate(DocumentEvent e) { /* nothing */ }
    @Override public void removeUpdate(DocumentEvent e) { /* nothing */ }
    @Override public void changedUpdate(DocumentEvent e) { /* nothing */ }
  }

  public static class InsertUpdate extends DocumentAdapter {
    private AdapterEventHandler<DocumentEvent> m_handler = null;
    public static InsertUpdate handle(AdapterEventHandler<DocumentEvent> handler) { InsertUpdate adapter = new InsertUpdate(); adapter.m_handler = handler; return adapter; }
    @Override public void insertUpdate(DocumentEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class RemoveUpdate extends DocumentAdapter {
    private AdapterEventHandler<DocumentEvent> m_handler = null;
    public static RemoveUpdate handle(AdapterEventHandler<DocumentEvent> handler) { RemoveUpdate adapter = new RemoveUpdate(); adapter.m_handler = handler; return adapter; }
    @Override public void removeUpdate(DocumentEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class InsertOrRemoveUpdate extends DocumentAdapter {
    private AdapterEventHandler<DocumentEvent> m_handler = null;
    public static InsertOrRemoveUpdate handle(AdapterEventHandler<DocumentEvent> handler) { InsertOrRemoveUpdate adapter = new InsertOrRemoveUpdate(); adapter.m_handler = handler; return adapter; }
    @Override public void insertUpdate(DocumentEvent e) { m_handler.handleWithRuntimeException(e); }
    @Override public void removeUpdate(DocumentEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  public static class ChangedUpdate extends DocumentAdapter {
    private AdapterEventHandler<DocumentEvent> m_handler = null;
    public static ChangedUpdate handle(AdapterEventHandler<DocumentEvent> handler) { ChangedUpdate adapter = new ChangedUpdate(); adapter.m_handler = handler; return adapter; }
    @Override public void changedUpdate(DocumentEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  // ---------------------------------------------------------------------------------------------------------------------------------------
  // AdjustmentListener
  // ---------------------------------------------------------------------------------------------------------------------------------------
  public static class AdjustmentValueChanged implements AdjustmentListener {
    private AdapterEventHandler<AdjustmentEvent> m_handler = null;
    public static AdjustmentValueChanged handle(AdapterEventHandler<AdjustmentEvent> handler) { AdjustmentValueChanged adapter = new AdjustmentValueChanged(); adapter.m_handler = handler; return adapter; }
    @Override public void adjustmentValueChanged(AdjustmentEvent e) { m_handler.handleWithRuntimeException(e); }
  }

  // ---------------------------------------------------------------------------------------------------------------------------------------
  // ListSelectionListener
  // ---------------------------------------------------------------------------------------------------------------------------------------
  public static class ValueChanged implements ListSelectionListener {
    private AdapterEventHandler<ListSelectionEvent> m_handler = null;
    public static ValueChanged handle(AdapterEventHandler<ListSelectionEvent> handler) { ValueChanged adapter = new ValueChanged(); adapter.m_handler = handler; return adapter; }
    @Override public void valueChanged(ListSelectionEvent e) { m_handler.handleWithRuntimeException(e); }
  }
  // @formatter:on
}

-4

无法从lambda表达式内访问默认方法。以下代码无法编译:

interface Formula {
    double calculate(int a);

    default double sqrt(int a) {
        return Math.sqrt(a);
    }
}
Formula formula = (a) -> sqrt( a * 100);

仅适用于功能接口(仅单个抽象方法+任意数量的默认方法),因此lambda表达式仅适用于抽象方法


1
抱歉,但我不明白这与问题有什么关系
fps
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.