import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
public class MyBean{
private String name = "redheart";
private String sex = "男";
private PropertyChangeSupport support = new PropertyChangeSupport(this);
public void setName(String newname){
String oldname = this.name;
this.name = newname;
support.firePropertyChange("name", oldname, newname);
}
public String getSex() {
return sex;
}
public void setSex(String newsex){
String oldsex = this.sex;
this.sex = newsex;
support.firePropertyChange("sex", oldsex, newsex);
}
public String getName(){
return name;
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
support.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
support.removePropertyChangeListener(listener);
}
}
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
public class UseMybean implements PropertyChangeListener
{
private static MyBean bean = new MyBean();
public static void main(String [] args)
{
UseMybean usebean = new UseMybean();
bean.addPropertyChangeListener(usebean);
bean.setName("bluesky");
bean.setSex("女");
System.out.println("打印我的名字:"+bean.getName());
System.out.println("打印我的姓别:"+bean.getSex());
}
public void propertyChange(PropertyChangeEvent e)
{
if (e.getSource() == bean)
{
//System.out.println("旧的名字:"+e.getPropertyName());
System.out.println("旧的"+e.getPropertyName()+":"+e.getOldValue().toString());
System.out.println("新的"+e.getPropertyName()+":"+e.getNewValue().toString());
}
}
}