2013-12-13 86 views

回答

0

採用經典的方法,接口繼承和組合如何執行動態地實現多類:

public interface Foo { 
    void foo(); 
} 

public class SomeFoo implements Foo { 
    public void foo() {} 
} 

public interface Bar { 
    void bar(); 
} 

public class SomeBar implements Bar { 
    public void bar() {} 
} 

public class SomeFooBar extends SomeFoo implements Bar { 
    private Bar bar; 

    public SomeFooBar(Bar bar) { 
     this.bar = bar; 
    } 

    public void bar() { 
    bar.bar(); 
    } 

}