Java回调函数

来源:互联网 发布:淘宝微商代理能挣钱吗? 编辑:程序博客网 时间:2024/06/03 23:18

 

public interface CallBackInterface {                 public void doSome();        public void executeMethod();    }
public class MethodB {        public double getTime(CallBackInterface callBack) {                  long start = System.currentTimeMillis();                  callBack.executeMethod();                  long end = System.currentTimeMillis();
                  System.out.println("cost time=" + (end - start));
 
                  return end - start;}}
public class MethodA {
public static void main(String[] args) {
 
MethodB b = new MethodB();
// 返回值d只和MethodB有关,和接口中方法的返回值无关
double d = b.getTime(new CallBackInterface() {
public void executeMethod() {
new MethodA().testMethod();
}
public void doSome() {
}
});
 
}
}



 

原创粉丝点击