首页 > 其他 > 详细

简单的监听器模式

时间:2018-01-07 16:45:03      阅读:194      评论:0      收藏:0      [点我收藏+]
package cn.thought;

import java.util.ArrayList;
import java.util.List;

public class Test {

    public static void main(String[] args) {
        Button btn = new Button();
        btn.addActionListened(new MyListener());
        btn.Pressed();
    }
}

/**
 * 创建一个按钮内部维护监听器
 * @author Administrator
 *
 */
class Button {
    /**
     * 对象的内部有个监听器集合
     */
    List<ActionListener> list = new ArrayList<ActionListener>();

    /**
     * 当按钮被按下
     */
    public void Pressed() {
        for (ActionListener ac : list) {
            ac.actionPerformed();
        }
    }
    /**
     * 为按钮添加监听器
     * @param l
     */
    public void addActionListened(ActionListener l) {
        this.list.add(l);
    }
}

/**
 * 实现监听器接口
 * @author Administrator
 *
 */
class MyListener implements ActionListener {
    public void actionPerformed() {
        System.out.println("鼠标被按下了");
    }
}
/**
 * 监听器接口
 * @author Administrator
 *
 */
interface ActionListener {
    public void actionPerformed();
}

 

简单的监听器模式

原文:https://www.cnblogs.com/yoyo198212/p/8228145.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!