多态--回调

来源:互联网 发布:linux 每10秒 shell 编辑:程序博客网 时间:2024/06/16 09:48

个人理解:基于多态实现,基本语法与多态相同,回调适用于一个完整的过程中某一步骤存在用户的差异,此处通过参数实现用户功能的调用,参数可以用接口或抽象类继承来实现,不过一般用接口。
如:

package cn.ldedu;/** * 测试回调 * @author Lenovo * */public class testCallBack {    public static void testDraw(MyInterface e/*此处也可不用接口,用抽象类继承来实现,推荐接口*/){        System.out.println("开始准备");        System.out.println("选择素材");        e.draw();                       //此处开始回调        System.out.println("结束");    }    public static void main(String[] args) {        testDraw(new draw());    }}interface MyInterface {    void draw();}class draw implements MyInterface{    @Override    public void draw() {        // TODO Auto-generated method stub        System.out.println("draw开始画图");    }}

运行截图:
这里写图片描述

原创粉丝点击