我构建了一个brainf..k 示例,展示了我能想到的所有不同可能性:
class OtherClass
{
public static void foo (final Anony x)
{
x.callback ();
}
}
public class Outer
{
public interface Anony
{
void callback ();
}
public class Inner implements Anony
{
public void callback ()
{
System.out.println ("Inner.callback");
}
}
public class InnerDerived implements Anony
{
public void callback ()
{
System.out.println ("InnerDerived.callback");
}
}
public static class StaticInner implements Anony
{
public void callback ()
{
System.out.println ("StaticInner.callback");
}
}
public Outer ()
{
OtherClass.foo (new Anony ()
{
public void callback ()
{
System.out.println ("Anony.callback");
}
});
OtherClass.foo (new Inner ());
OtherClass.foo (new Inner ()
{
@Override
public void callback ()
{
System.out.println ("Anony.Inner.callback");
}
});
OtherClass.foo (new InnerDerived ());
OtherClass.foo (new InnerDerived ()
{
@Override
public void callback ()
{
System.out.println ("Anony.InnerDerived.callback");
}
});
OtherClass.foo (new StaticInner ());
OtherClass.foo (new StaticInner ()
{
@Override
public void callback ()
{
System.out.println ("Anony.StaticInner.callback");
}
});
}
}
预期的输出应该是:
Anony.callback
Inner.callback
Anony.Inner.callback
InnerDerived.callback
Anony.InnerDerived.callback
StaticInner.callback
Anony.StaticInner.callback