采用回调接口!小例子:
- interface PrintInter {
- public void print(String text);
- }
- class PrintClass {
- public void printer(PrintInter inter){
- System.out.println("回调前");
- inter.print("回调中...");
- System.out.println("回调后");
- }
- }
- class CallBackDemo {
- public static void main(String[] args) {
- PrintClass print = new PrintClass();
- print.printer(new PrintInter(){
- public void print(String text){
- System.out.println(text);
- System.out.println("这里可以做任何事情!!");
- }
- });
- }
- }
复制代码 |