gpt4 book ai didi

java - 在 Java 中是否有使用方法引用 JAVA 覆盖方法的缩写形式?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:01:06 24 4
gpt4 key购买 nike

我一直遇到这样的情况,这真的很方便

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
}

这有可能吗?因为非功能性接口(interface)的整个覆盖非常令人沮丧。

最佳答案

没有这样的语言功能,但如果您必须经常实现一个接口(interface),以至于这种冗长变得相关,您可以编写自己的适配器。

例如,使用下面的适配器,您可以编写

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

关于java - 在 Java 中是否有使用方法引用 JAVA 覆盖方法的缩写形式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52827825/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com