Java——回调
发布日期:2021-05-10 16:26:36 浏览次数:14 分类:精选文章

本文共 3559 字,大约阅读时间需要 11 分钟。

������������������������

  • ���������������
  • ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

    1.1 ������������

    ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

    ������������������A���������a()���������B���������b()���������a()���������������������b()���������������������������������������������������b()���������������������������������������������������������������������b()������������������������������������a()������������������������������������������������������������������������������

    1.2 ������������

    ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

    ������������������������������������������������������������������������������������������������������������������������������Java������Future���Callable������������������������������������������������

    1.3 ������������

    ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

    ������������������������������������������������������������A���������a()���������B���������b()������B���������b()���������������������������������A���callback()������������������������������������������������������������������������������������

    1. ������������
    2. ���������������������������������������������tellAnswer(int answer)������������������������������������������������������������������������������������������������������

    3. ���������������������
    4. public interface Callback {
      public void tellAnswer(int answer);
      }
      1. ������������������
      2. public class Teacher implements Callback {
        private Student student;
        public Teacher(Student student) {
        this.student = student;
        }
        public void askQuestion() {
        student.resolveQuestion(this);
        }
        @Override
        public void tellAnswer(int answer) {
        System.out.println("���������������������������������" + answer);
        }
        }
        1. ���������������������
        2. public class Student {
          public void resolveQuestion(Callback callback) {
          // ���������������������������
          try {
          Thread.sleep(3000);
          } catch (InterruptedException e) {
          e.printStackTrace();
          }
          // ������������������������
          callback.tellAnswer(10080); // ���������10080���������������������������������������������������������
          }
          }
          1. ������������������������
          2. public class CallbackTest {
            @Test
            public void testCallback() {
            Student student = new Student();
            Teacher teacher = new Teacher(student);
            teacher.askQuestion();
            }
            }
            1. ������������
            2. ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������Student������������List������������������������

              ���������������������������������������������������������������������������������������������������������������������������������������������������������������������"this"���������������������������������������������������������������

    上一篇:Java——abstract 和 interface
    下一篇:Java——重写与重载

    发表评论

    最新留言

    不错!
    [***.144.177.141]2025年04月02日 05时45分39秒