EventListener,监听注册

  1. import java.util.*;
  2. public class TestGirl {
  3.     public static void main(String[] args) {
  4.         Girl g = new Girl("秋香");
  5.         Boy1 boy1 = new Boy1("zhaojun");
  6.         g.addEmotionListener(boy1);
  7.         g.fire();
  8.         g.removeEmotionListener(boy1);
  9.         
  10.         Boy2 boy2 = new Boy2("ankun");
  11.         g.addEmotionListener(boy2);
  12.         g.fire();
  13.         g.removeEmotionListener(boy2);
  14.         
  15.     }
  16. }
  17. /*
  18.  * 事件: 封装事件源
  19.  */
  20. class EmotionEvent extends EventObject{
  21.     public EmotionEvent(Object source){
  22.         super(source);
  23.     }
  24. }
  25. /*
  26.  * 监听器: 处理事件
  27.  */
  28. interface EmotionListener extends EventListener{
  29.     void whatCanIdoWhenGirlSad(EmotionEvent event);
  30.     void whatCanIdoWhenGirlHappy(EmotionEvent event);
  31. }
  32. /*
  33.  * 事件源:产生事件
  34.  */
  35. class Girl{
  36.     private String name;
  37.     private EmotionListener listener;
  38.     public Girl(String name){
  39.         this.name = name;
  40.     }
  41.     public String getName(){
  42.         return this.name;
  43.     }
  44.     public void addEmotionListener(EmotionListener listener){
  45.         System.out.println(name +" said: 找到一个男朋友!");
  46.         this.listener = listener;
  47.     }
  48.     public void removeEmotionListener(EmotionListener listener){
  49.         System.out.println(name +" said: 离开你我怎么活呀!");
  50.         listener = null;
  51.     }
  52.     public void fire(){
  53.         for(int i=1; i<4; i++){
  54.             System.out.println("Day" + i + ":");
  55.             if(i%2 == 0){
  56.                 System.out.println(name + " said: " + "今天我不太开心!");
  57.                 EmotionEvent event = new EmotionEvent(this);
  58.                 listener.whatCanIdoWhenGirlSad(event);
  59.             }else{
  60.                 System.out.println(name + " said: " + "今天我好开心!");
  61.                 EmotionEvent e = new EmotionEvent(this);
  62.                 listener.whatCanIdoWhenGirlHappy(e);
  63.             }
  64.         }
  65.     }
  66. }
  67. class Boy1 implements EmotionListener{
  68.     private String name;
  69.     public Boy1(String name){
  70.         this.name = name;
  71.     }
  72.     @Override
  73.     public void whatCanIdoWhenGirlHappy(EmotionEvent event) {
  74.         Object obj = event.getSource();
  75.         Girl g = (Girl)obj;
  76.         System.out.println(name + " said to "+g.getName() +": 你开心我也挺开心的!");
  77.     }
  78.     @Override
  79.     public void whatCanIdoWhenGirlSad(EmotionEvent event) {
  80.         Girl g = (Girl)event.getSource();
  81.         System.out.println(name + " said to "+g.getName()+ ": 你不开心我挺不愉快的!!");
  82.     }
  83. }
  84. class Boy2 implements EmotionListener{
  85.     private String name;
  86.     public Boy2(String name){
  87.         this.name = name;
  88.     }
  89.     @Override
  90.     public void whatCanIdoWhenGirlHappy(EmotionEvent event) {
  91.         Girl g = (Girl)event.getSource();
  92.         System.out.println(name + " said to "+ g.getName() +": 你开心我挺不开心的!");
  93.     }
  94.     @Override
  95.     public void whatCanIdoWhenGirlSad(EmotionEvent event) {
  96.         Girl g = (Girl)event.getSource();
  97.         System.out.println(name + " said to "+ g.getName() +": 你不开心我挺愉快的!!");
  98.     }
  99. }
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值