[关闭]
@XQF 2017-02-15T11:46:13.000000Z 字数 653 阅读 1147

回调总结

java


1.快动作

  1. interface Hehe {
  2. void print(String str);
  3. }
  4. public class Test {
  5. public void print(String str, Hehe hehe) {
  6. if (hehe != null) {
  7. hehe.print(str);
  8. }
  9. }
  10. public static void main(String[] args) {
  11. Test t = new Test();
  12. t.print("测试", new Hehe() {
  13. @Override
  14. public void print(String str) {
  15. System.out.println(str + "结束");
  16. }
  17. });
  18. }
  19. }

2.慢动作

  1. interface Hehe {
  2. void print(String str);
  3. }
  4. class Haha implements Hehe {
  5. @Override
  6. public void print(String str) {
  7. System.out.println(str + "结束");
  8. }
  9. }
  10. public class Test {
  11. public void print(String str, Hehe hehe) {
  12. if (hehe != null) {
  13. hehe.print(str);
  14. }
  15. }
  16. public static void main(String[] args) {
  17. Test t = new Test();
  18. t.print("测试", new Haha());
  19. }
  20. }

3.函数指针

说是这样的回调似乎是实现了C++里面的函数指针的问题。好像是,,。,说的也对。
接口的引用就相当于是函数指针。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注