Java では、次の例のようにローカル抽象クラスを定義できます。
public class Foo {
public void foo() {
abstract class Bar { // Bar is a local class in foo() ...
abstract void bar();
}
new Bar() { // ... and can be anonymously instantiated
void bar() {
System.out.println("Bar!");
}
}.bar();
}
}
何らかの理由で、ローカル クラスの代わりに「ローカル インターフェイス」を定義しようとすると、次のようになります。
public class Foo {
public void foo() {
interface Bar { // Bar was supposed to be a local interface...
void bar();
}
new Bar() { // ... to be anonymously instantiated
void bar() {
System.out.println("Bar!");
}
}.bar();
}
}
Java は、「メンバー インターフェイス Bar は、最上位のクラスまたはインターフェイス内でしか定義できない」と不満を漏らしています。これには理由がありますか?それとも、私が犯した間違いを見逃していますか?