java回调函数

C++回调函数通过函数指针实现,Java中没有函数指针,可以通过接口实现。

 1 interface IShow{
 2     public void show(String str);
 3 }
 4 
 5 class ShowA implements IShow{    
 6     public void show(String str) {
 7         System.out.println(str+" is very high!");        
 8     }    
 9 };
10 
11 class ShowB implements IShow{    
12     public void show(String str) {
13         System.out.println(str+" is very rich!");        
14     }    
15 };
16 
17 class ShowC implements IShow{    
18     public void show(String str) {
19         System.out.println(str+" is very handsome!");        
20     }    
21 };
1         String str = "Jack";
2         IShow showA = new ShowA();
3         IShow showB = new ShowB();
4         IShow showC = new ShowC();
5         showA.show(str);
6         showB.show(str);
7         showC.show(str);

运行结果:

 

可通过接口对相应功能进行扩展和更新。

posted @ 2014-12-16 10:52  丛林小阁楼  阅读(141)  评论(0编辑  收藏  举报