java中的PropertyChangeSupport与PropertyChangeListener
程序员文章站
2022-03-01 23:14:09
...
java中的PropertyChangeSupport与PropertyChangeListener配合使用能够实现,绑定属性会在属性值发生变化时,通知所有相关的监听器。
javaBean:
package com.wang.test.property; import java.beans.PropertyChangeListener; import java.beans.PropertyChangeSupport; public class TestBean { protected final PropertyChangeSupport support = new PropertyChangeSupport(this); private String str; public String getStr() { return str; } public void setStr(String str) { support.firePropertyChange("str", this.str, str); this.str = str; } public void addPropertyChangeListener(PropertyChangeListener listener) { support.addPropertyChangeListener(listener); } public void removePropertyChangeListener(PropertyChangeListener listener) { support.removePropertyChangeListener(listener); } }
client:
package com.wang.test.property; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; public class PropertyChangeSupportTest { public static void main(String[] args) { TestBean test = new TestBean(); test.setStr("ddddd"); test.addPropertyChangeListener(new PropertyChangeListener1()); test.setStr("yyyy"); //test.addPropertyChangeListener(new PropertyChangeListener2()); //test.setStr("hhhhhhf"); } public static class PropertyChangeListener1 implements PropertyChangeListener{ @Override public void propertyChange(PropertyChangeEvent evt) { System.out.println( "1: " + evt.getPropertyName() + " " + evt.getNewValue() + " " + evt.getOldValue()); } } public static class PropertyChangeListener2 implements PropertyChangeListener{ @Override public void propertyChange(PropertyChangeEvent evt) { System.out.println( "2: " + evt.getPropertyName() + " " + evt.getNewValue() + " " + evt.getOldValue()); } } }
推荐阅读
-
RMAN中nocatalog与catalog的区别
-
C#中调用Windows系统服务exe程序的工具类与重启服务的流程
-
Ubuntu中Nginx的安装与配置详细说明
-
apmserv中修改mysql的root与phpmyadmin密码方法介绍_PHP教程
-
C#中异步编程4async与await异步程序开发的实例分析
-
Java中final关键字的使用与注意总结
-
PHP与MySQL开发中页面乱码的产生与解决_PHP教程
-
php中的四舍五入函数代码(floor函数、ceil函数、round与intval),ceilintval
-
JS中call与apply的作用及区别 jscallapply执行上下文
-
浅析Java中的GC垃圾回收器的意义及与GC的交互