Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 为什么运行结果会是这样?
zhjdenis





发贴: 15
积分: 0
于 2007-10-03 20:38 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
使用限制属性,运行正常,但是为什么第二次由于属性被拒绝后连续打印了两个
“property: weightwill be changed”?
运行结果如下
property: weightwill be changed
bean's value: weight: 20
second change:
property: weightwill be changed
property: weightwill be changed
property: weight is veto
bean's value: weight: 20

//以下是代码
package 限制属性;
import java.beans.*;
public class Mybean {
private int weight;
private transient VetoableChangeSupport veto=new VetoableChangeSupport(this);
public int getweight()
{
   return weight;
}
public void setweight(int weight) throws java.beans.PropertyVetoException
{
   int oldweight=this.weight;
   veto.fireVetoableChange("weight",new Integer(oldweight),new Integer(weight));
   this.weight=weight;
}
public String tostring()
{
   String s="bean's value: ";
   s=s+"weight: "+weight;
   return s;
}
public synchronized void add(VetoableChangeListener x)
{
  veto.addVetoableChangeListener(x);
}
}
//以下是主测试程序代码
package 限制属性;
import java.beans.*;
public class Propertydemo {
  public static void main(String[] args) {
    // TODO Auto-generated method stub
Mybean bean1=new Mybean();
bean1.add(new Myvetochangelistener());
System.out.println(bean1.tostring());
System.out.println("first change: ");
try
{
  bean1.setweight(20);
}
catch(PropertyVetoException ex)
{
  System.out.println(ex.getMessage());
}
System.out.println(bean1.tostring());
System.out.println("second change: ");
try
{
  bean1.setweight(150);
}
catch(PropertyVetoException ex1)
{
  System.out.println(ex1.getMessage());
}
System.out.println(bean1.tostring());
  }

}
//以下是否决监听器的代码
package 限制属性;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyVetoException;
import java.beans.VetoableChangeListener;
public class Myvetochangelistener implements VetoableChangeListener {

  public void vetoableChange(PropertyChangeEvent y)throws PropertyVetoException
  {
   System.out.println("property: "+y.getPropertyName()+" will be changed");
   if(((Integer)y.getNewValue()).intValue()>100)
   {
     throw new PropertyVetoException("property: "+y.getPropertyName()+" is veto",y);
   }
  }
      

}




从Coding Fan到真正的技术专家

话题树型展开
人气 标题 作者 字数 发贴时间
4463 为什么运行结果会是这样? zhjdenis 2358 2007-10-03 20:38

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923