• 回调方法是什么及其理解


    回调的思想

    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

  • 相关阅读:
    数组的完全随机排列算法
    css超出2行部分省略号...
    前端面试题精华总结
    在地址栏输入网址后页面是如何呈现的?
    document.write和innerHTML的区别
    js运算符单竖杠“|”与“||”的用法和作用介绍
    border:none与border:0的区别
    如何实现浏览器内多个标签页之间的通信?
    js 关键字 in 的使用方法
    msyql: navicat 连接时msyql遇到的问题
  • 原文地址:https://www.cnblogs.com/myseries/p/10980544.html
Copyright © 2020-2023  润新知