package data; import java.util.HashMap; import java.util.Map; public class Good { CallBack callBack; public void setCallBack(CallBack callBack) { this.callBack=callBack; } private void doMyThing(){ System.out.println("我做我自己的事情"); } public void doSomeThing(){ this.doMyThing(); System.out.println("做完了我的事情"); System.out.println("开始回调"); this.callBack.show(); } public static void main(String[] args) { Good good=new Good(); good.setCallBack(new CallBack() { @Override void toDo(Map<String, String> map) { map.put("people", "哈哈"); map.put("age", "100"); } }); good.doSomeThing(); } } abstract class CallBack { Map<String, String> map = new HashMap<String, String>(); public void show() { this.toDo(map); map.put("type", "人类"); System.out.println(map); } abstract void toDo(Map<String, String> map); }
时间: 2024-10-10 09:24:57