package innerclasstest;
interface Demo {
}
class Bar {
public void call() {
Foo f = new Foo();
f.doStuff(new Demo() {
public void fall() {
System.out.println("In method args...");
}
});
}
}
class Foo {
public void doStuff(Demo demo) {
System.out.println("In stuff");
}
}
public class ClassArg {
public static void main(String[] args) {
Bar b = new Bar();
b.call();
}
}
在上面的示例中,我們如何調用匿名類方法Fall。有什麼辦法可以調用這個方法。我不知道我應該選擇哪種方法來調用這個方法。如何在Java中調用匿名類方法
使用接口 – SLaks
您已經發現匿名類應該是IMO的幾個原因之一,「被認爲是有害的」。它們不能被重用,並且通常會導致大量的複製和粘貼代碼。因人而異。 – user949300