1.总体更新信息、传递到不同的局部,这里是高内聚
Student student_xuxiaodong = new Student_xuxiaodong(); Teacher teacherA = new Teacher(student_xuxiaodong); Log.i("CallBack"," start AA .... " ); teacherA.askQuestion(); Student mStudent_XuChi = new Student_XuChi(); Teacher teacherB = new Teacher(mStudent_XuChi); Log.i("CallBack"," start BB .... " ); teacherB.askQuestion(); Student mStudent_XuRong = new Student_XuRong(); Teacher teacherC = new Teacher(mStudent_XuRong); Log.i("CallBack"," start CC .... " ); teacherC.askQuestion();
2.解藕
//学生告诉老师信息 类 package com.example.myapplication; public interface Student { public void studentTellTeacherInfo(Callback callback); }
//老师接收学生 信息 类 package com.example.myapplication; import android.util.Log; public class Teacher implements Callback { private Student student; public Teacher(Student student) { this.student = student; } @Override public void answerTeacherTime(String name ,String work_time) { //老师只需要回答多久? Log.i("CallBack"," 姓名: " +name); Log.i("CallBack"," 知道了,你做作业用时 " +work_time); } public void askQuestion(){ student.studentTellTeacherInfo(this); } }
// 学生 Student_XuChi
package com.example.myapplication; import android.util.Log; public class Student_XuChi implements Student { @Override public void studentTellTeacherInfo(Callback callback) { try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } callback.answerTeacherTime("Xu_Chi","5000秒"); Log.i("CallBack"," start Student_XuChi " + callback.hashCode()); } }
//学生 Student_XuRong
package com.example.myapplication; import android.util.Log; public class Student_XuChi implements Student { @Override public void studentTellTeacherInfo(Callback callback) { try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } callback.answerTeacherTime("Xu_Chi","5000秒"); Log.i("CallBack"," start Student_XuChi " + callback.hashCode()); } }
// 学生 Student_xuxiaodong
package com.example.myapplication; import android.util.Log; public class Student_xuxiaodong implements Student { @Override public void studentTellTeacherInfo(Callback callback) { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } callback.answerTeacherTime("xuxiaodong","3000秒"); Log.i("CallBack"," start Student_xuxiaodong " + callback.hashCode()); } // @Override // public void answerTeacherTime(String name, String work_time) { // try { // Thread.sleep(3000); // } catch (InterruptedException e) { // e.printStackTrace(); // } // Callback. // } }
//执行情况
C:\Users\admin>adb logcat -s CallBack
--------- beginning of system
--------- beginning of main
01-30 10:20:37.768 4494 4494 I CallBack: start AA ....
01-30 10:20:40.770 4494 4494 I CallBack: 姓名: xuxiaodong
01-30 10:20:40.770 4494 4494 I CallBack: 知道了,你做作业用时 3000秒
01-30 10:20:40.771 4494 4494 I CallBack: start Student_xuxiaodong 15652383
01-30 10:20:40.771 4494 4494 I CallBack: start BB ....
01-30 10:20:45.772 4494 4494 I CallBack: 姓名: Xu_Chi
01-30 10:20:45.772 4494 4494 I CallBack: 知道了,你做作业用时 5000秒
01-30 10:20:45.772 4494 4494 I CallBack: start Student_XuChi 99143020
01-30 10:20:45.772 4494 4494 I CallBack: start CC ....
01-30 10:20:53.773 4494 4494 I CallBack: 姓名: Xu_Chi
01-30 10:20:53.773 4494 4494 I CallBack: 知道了,你做作业用时 8000秒
01-30 10:20:53.773 4494 4494 I CallBack: start Student_XuRong 4099893