回調方法是什么及其理解


回調的思想

  1. 類A的a()方法調用類B的b()方法
  2. 類B的b()方法執行完畢主動調用類A的callback()方法

代碼分析

public interface Callback {
    public void tellAnswer(int answer);
}
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);
    }
}
public interface Student {
    public void resolveQuestion(Callback callback);
}
public class Ricky implements Student {
    @Override
    public voidresolveQuestion(Callback callback) {
        // 模擬解決問題
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            
        }
        // 回調,告訴老師作業寫了多久
        callback.tellAnswer(3);
    }
}

測試

@Test
public void testCallBack() {
    Student student = new Ricky();
    Teacher teacher = new Teacher(student);
    
    teacher.askQuestion();
}

Student也可以使用匿名類定義,更加簡潔:

@Test
public void testCallBack2() {
    Teacher teacher = new Teacher(new Student() {
        
        @Override
        public void resolveQuestion(Callback callback) {
            callback.tellAnswer(3);
        }
    });
    
    teacher.askQuestion();
}

分析

  Teacher 中,有一個解決問題的對象:Student,在Student中解決問題之后,再通過引用調用Teacher中的tellAnswer接口,所以叫回調

 

同步、異步調用

上面的例子是同步回調,下面介紹異步調用

public interface Callback {
    void tellAnswer(int answer);
}
public class Teacher implements Callback{
    private Student student;
    public Teacher (Student student) {
        this.student = student;
    }

    public void askQuestion() {
        //student.resolveQuestion(this); //此處是同步回調。
        new Thread(()-> student.resolveQuestion(this)).start();  //這里實現了異步,此處的this也可以用Teacher.this代替,
                                                                // 如果不用lambda表達式,用匿名內部類創建new Runnable()則一定要用Teacher.this
    }


    @Override
    public void tellAnswer(int answer) {
        System.out.println("你的答案是:" + answer + "正確");
    }
}
public interface Student {
    public void resolveQuestion(Callback callback);
}
public class Ricky implements Student {

    @Override
    public void resolveQuestion(Callback callback) {
        try {
            Thread.sleep(3000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //回調,告訴老師問題的答案
        callback.tellAnswer(3);
    }
}

測試

public class CallbackTest {
    @Test
    public void testCallback() {
        Student student = new Ricky();
        Teacher teacher = new Teacher(student);

        teacher.askQuestion();
        System.out.println("end");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

 

出處:  

  https://cloud.tencent.com/developer/article/1351239

  https://blog.csdn.net/qq_31617121/article/details/80861692


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM