メソッド参照JAVAを使用してJavaのメソッドをオーバーライドするための任意の短縮形はありますか?

ジョン:

私は、これは本当に便利になるような状況を持っておきます

component.addWindowListener() { new WindowListener() {
        // quick overriding to make syntax less verbose
        windowClosing(e) -> Foo::doFoo;

        windowActivated(e) -> Foo::doFoo;
    }
}

現在、これは主に次のようになります。

component.addWindowListener() new WindowListener() {
    @Override
    public void windowClosing(WindowEvent e) {
        Foo.doFoo(e);
    }

    @Override
    public void windowActivated(WindowEvent e) {
        Foo.doFoo(e);
    }
}

どこにいくつかの機能への方法基準点:

public static void doFoo(WindowEvent e) {
    // does code
}

この可能性のようなものですか?全体が非機能インタフェースのオーバーライドので、非常に、非常にイライラさせられます。

ホルガー:

そこには、このような言語機能はありませんが、あなたはそう頻繁にこの冗長性が関連になるというインタフェースを実装する必要がある場合、あなたは独自のアダプタを書くことができます。

例えば、以下のアダプタで、あなたが書くことができます

f.addWindowListener(WindowAdapter.window()
    .onClosing(ev -> ev.getWindow().dispose())
    .onClosed(ev -> System.out.println("closed"))
);

またはの力を利用import static

f.addWindowListener(window().onClosing(ev -> System.out.println("closing")));

そう、あなたの例で滞在します

f.addWindowListener(window().onClosing(Foo::doFoo).onActivated(Foo::doFoo));

アダプタ:

public class WindowAdapter implements WindowListener {
    static Consumer<WindowEvent> NO_OP = ev -> {};
    public static WindowAdapter window() {
        return new WindowAdapter(NO_OP, NO_OP, NO_OP, NO_OP, NO_OP, NO_OP, NO_OP);
    }
    final Consumer<WindowEvent> opened, closing, closed,
        iconified, deiconified, activated, deactivated;

    public WindowAdapter(Consumer<WindowEvent> opened, Consumer<WindowEvent> closing,
        Consumer<WindowEvent> closed, Consumer<WindowEvent> iconified,
        Consumer<WindowEvent> deiconified, Consumer<WindowEvent> activated,
        Consumer<WindowEvent> deactivated) {
        this.opened = opened;
        this.closing = closing;
        this.closed = closed;
        this.iconified = iconified;
        this.deiconified = deiconified;
        this.activated = activated;
        this.deactivated = deactivated;
    }
    public WindowAdapter onOpened(Consumer<WindowEvent> c) {
        Objects.requireNonNull(c);
        return new WindowAdapter(opened==NO_OP? c: opened.andThen(c),
            closing, closed, iconified, deiconified, activated, deactivated);
    }
    public WindowAdapter onClosing(Consumer<WindowEvent> c) {
        Objects.requireNonNull(c);
        return new WindowAdapter(opened, closing==NO_OP? c: closing.andThen(c),
            closed, iconified, deiconified, activated, deactivated);
    }
    public WindowAdapter onClosed(Consumer<WindowEvent> c) {
        Objects.requireNonNull(c);
        return new WindowAdapter(opened, closing, closed==NO_OP? c: closed.andThen(c),
            iconified, deiconified, activated, deactivated);
    }
    public WindowAdapter onIconified(Consumer<WindowEvent> c) {
        Objects.requireNonNull(c);
        return new WindowAdapter(opened, closing, closed,
          iconified==NO_OP? c: iconified.andThen(c), deiconified, activated, deactivated);
    }
    public WindowAdapter onDeiconified(Consumer<WindowEvent> c) {
        Objects.requireNonNull(c);
        return new WindowAdapter(opened, closing, closed, iconified,
            deiconified==NO_OP? c: deiconified.andThen(c), activated, deactivated);
    }
    public WindowAdapter onActivated(Consumer<WindowEvent> c) {
        Objects.requireNonNull(c);
        return new WindowAdapter(opened, closing, closed, iconified,
            deiconified, activated==NO_OP? c: activated.andThen(c), deactivated);
    }
    public WindowAdapter onDeactivated(Consumer<WindowEvent> c) {
        Objects.requireNonNull(c);
        return new WindowAdapter(opened, closing, closed, iconified,
            deiconified, activated, deactivated==NO_OP? c: deactivated.andThen(c));
    }
    @Override public void windowOpened(WindowEvent e) { opened.accept(e); }
    @Override public void windowClosing(WindowEvent e) { closing.accept(e); }
    @Override public void windowClosed(WindowEvent e) { closed.accept(e); }
    @Override public void windowIconified(WindowEvent e) { iconified.accept(e); }
    @Override public void windowDeiconified(WindowEvent e) { deiconified.accept(e); }
    @Override public void windowActivated(WindowEvent e) { activated.accept(e); }
    @Override public void windowDeactivated(WindowEvent e) { deactivated.accept(e); }
}

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=204644&siteId=1