编写回调方式,需要写接口
public interface TestWriteName {
public int write(int a, int b, int result);
}
需要用回调的类要继承此接口。
处理类:
public class TestComeTrue{
public void addTest(int a, int b, TestWriteName t) {
int result = a + b;
t.write(a, b, result);
}
}
实现类:
public class TestMain implements TestWriteName {
@Override
public int write(int a, int b, int result) {
System.out.println("测试输出结果a:" + a + ",b:" + b + ",result:" + result);
return result;
}
public static void main(String[] args) {
TestMain testMain = new TestMain();
testMain.test();
}
public void test() {
new TestComeTrue().addTest(1, 2, new TestMain());
}
}