interface Foo { void say(); } public class lzwCode { public Foo f = new Foo(){ public void say() { System.out.println("Say Foo!"); } }; public Foo test() { return new Foo(){ public void say() { System.out.println("Test Say Foo!"); } }; } public static void main(String [] args) { lzwCode lc = new lzwCode(); lc.f.say(); lc.test().say(); } }
//经典实例,来自thining in java,有改动 interface Service { void mothodA(); void mothodB(); } interface ServiceFactory{ Service getService(); } //1 class ImplementsActionA implements Service { private ImplementsActionA() { } public void mothodA() { System.out.println("ImplementsActionA 实现 mothodA()方法"); } public void mothodB() { System.out.println("ImplementsActionA 实现 mothodB()方法"); } public static ServiceFactory factory = new ServiceFactory(){ public Service getService() { return new ImplementsActionA(); } }; } //2 class ImplementsActionB implements Service { private ImplementsActionB() { } public void mothodA() { System.out.println("ImplementsActionB 实现 mothodA()方法"); } public void mothodB() { System.out.println("ImplementsActionB 实现 mothodB()方法"); } public static ServiceFactory factory = new ServiceFactory(){ public Service getService() { return new ImplementsActionB(); } }; } //工厂 public class lzwCode { public static void serviceConsumer(ServiceFactory face) { Service ser = face.getService(); ser.mothodA(); ser.mothodB(); } public static void main(String [] args) { serviceConsumer(ImplementsActionA.factory); serviceConsumer(ImplementsActionB.factory); } }控制台结果:
class Outer { public static int i = 404; protected static class Inner { int i = 110; String name; Inner(String name) { this.name = name; } void sayHello() { System.out.println("Hello "+name); Outer.i++; this.i++; } } public Inner getInner(String name) { return new Inner(name); } } public class lzwCode { public static void main(String [] args) { Outer.Inner inn = new Outer.Inner("梅西"); inn.sayHello(); System.out.println(Outer.i); System.out.println(inn.i); } }
interface InterInner { void sayHello(); class Inner implements InterInner { public void sayHello() { System.out.println("这是一个接口内部类"); } } } public class lzwCode { public static void main(String [] args) { InterInner.Inner demo = new InterInner.Inner(); demo.sayHello(); } }
这是一个接口内部类
class Outer { public void OuterMothod() { System.out.println("outer mothod!"); } class Inner { public void sayHello() { System.out.println("Hello Barcelona"); } } class InnerA extends Outer{ public void OuterMothod() { System.out.println("InnerA mothod!"); } public void readMe() { System.out.println("InnerA O(∩_∩)O"); } } class InnerB extends Inner { public void sayHello() { System.out.println("InnerB Hello Barcelona"); } public void readYou() { System.out.println("InnerB O(∩_∩)O"); } } } public class lzwCode { public static void main(String [] args) { Outer outer = new Outer(); Outer.Inner inn = outer.new Inner(); inn.sayHello(); System.out.println("==========1==========="); Outer.InnerA innA = outer.new InnerA(); innA.OuterMothod(); innA.readMe(); System.out.println("==========2==========="); Outer.InnerB innB = outer.new InnerB(); innB.sayHello(); innB.readYou(); } }