javabean的简单实现



1.理解JavaBean的概念和特性。 

2.掌握编写JavaBean的方法和注意事项。

3. 掌握JavaBean事件模型的实现。

1.构造监听器类VetoableScoreListener

import java.beans.PropertyChangeEvent;

import java.beans.PropertyVetoException;

import java.beans.VetoableChangeListener;

 

public classVetoableScoreListener implementsVetoableChangeListener

{

   publicvoidVetoableChange(PropertyChangeEvent event)throwsPropertyVetoException

   {

    Object newValue=event.getNewValue();

    int Value=((Integer)newValue).intValue();

    if (Value<0||Value>100)

     {

       throw newPropertyVetoException("成绩不符合要求",event);

     }

   }

@Override

public voidvetoableChange(PropertyChangeEvent evt)

     throws PropertyVetoException {

   // TODO Auto-generated method stub

  

}

}

2.构造限制类retoableScore

import java.beans.PropertyChangeEvent;

import java.beans.PropertyVetoException;

import java.beans.VetoableChangeListener;

import java.beans.VetoableChangeSupport;

 

public classretoableScoreextends Score

{

   privateVetoableChangeSupportsupport=newVetoableChangeSupport(this);

   privateintvalue;

  publicvoidaddVetoableChangeListener(VetoableChangeListenerlistener)

   {

    support.addVetoableChangeListener(listener);

   }

  publicvoidremoveVetoableChangeListener(VetoableChangeListenerlistener)

   {

     support.removeVetoableChangeListener(listener);

   }

   publicvoidsetValue(intvalue)

   {

     PropertyChangeEventevent=newPropertyChangeEvent(this,"value",newInteger(value),newInteger(value));

     try

     {

       

       support.fireVetoableChange(event);

       super.value=value;

     }

     catch (Exception e)

     {

       System.out.println(e.toString());

     }

   }

}

3.构造score类以及测试类

public classScore

{

  protectedintvalue;

 

  Score()

  {

   value=0;

  }

  publicScore(intvalue)

  {

   this.value=value;

  }

  publicintget()

  {

   return value;

  }

  publicvoidset(intvalue)

  {

    this.value=value;

  }

public voidaddVetoableChangeListener(VetoableScoreListener listener){

   // TODO Auto-generated method stub

  

}

public classTest{

   public static void main(String args[])

   {

     retoableScore score=newretoableScore();

     VetoableScoreListener listener=newVetoableScoreListener();

     try

     {

        

        

        score.setValue(10);

        PropertyChangeEvent event=newPropertyChangeEvent(score, "value", 10,10);

        listener.VetoableChange(event);

        System.out.println(score.get());

        

        score.setValue(-10);

        PropertyChangeEvent event1=newPropertyChangeEvent(score, "value", -10,-10);

        listener.VetoableChange(event1);

        System.out.println(score.get());

        

        

     }

     catch (Exception e)

     {

        System.out.println(e.toString());

     }

     

   }

 

 

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值