-3

我的问题是如果用户尝试更改 18 岁以上的人的年龄并抛出此异常“应该转移成人”,我的问题是如何更改(年龄 >=18),因为到目前为止我所拥有的确实允许我更改没有抛出异常

     import java.beans.PropertyChangeEvent;
     import java.beans.PropertyVetoException;
     import java.beans.VetoableChangeListener;

    public class Registrar implements VetoableChangeListener {
         int age;
    @Override
    public void vetoableChange(PropertyChangeEvent event)
        throws PropertyVetoException {
    if (event.getPropertyName().equals("age")) {
        String proposedAge = (String)event.getNewValue();
              Integer.parseInt(proposedAge);
          if (age >=18 ) {
               throw new PropertyVetoException("Adult  should be  transferred.", event);
        }
    }
}

}

4

2 回答 2

0

您忘记设置年龄:

     import java.beans.PropertyChangeEvent;
     import java.beans.PropertyVetoException;
     import java.beans.VetoableChangeListener;

    public class Registrar implements VetoableChangeListener {
         int age;
    @Override
    public void vetoableChange(PropertyChangeEvent event)
        throws PropertyVetoException {
    if (event.getPropertyName().equals("age")) {
        String proposedAge = (String)event.getNewValue();
            age = Integer.parseInt(proposedAge);
          if (age >=18 ) {
               throw new PropertyVetoException("Adult  should be  transferred.", event);
        }
    }
}

}

于 2013-08-30T22:08:12.083 回答
0

首先你需要设置年龄:

age = Integer.parseInt(proposedAge);

然后我建议你使用日志或控制台

System.out.println("Adult  should be  transferred");

.

import java.beans.PropertyChangeEvent;
import java.beans.PropertyVetoException;
import java.beans.VetoableChangeListener;

public class Registrar implements VetoableChangeListener {
   int age;
   @Override
   public void vetoableChange(PropertyChangeEvent event)
       throws PropertyVetoException {
       if (event.getPropertyName().equals("age")) {
           String proposedAge = (String)event.getNewValue();
           age = Integer.parseInt(proposedAge);
           if (age >=18 ) {
               System.out.println("Adult  should be  transferred");
           }
       }
    }
}
于 2013-08-30T22:01:46.913 回答