在这个程序中,我将属性存储到arraylist。
public static void main(String [] args) throws Exception{
Property property1 = new Property(24,"Boston Australia",3,45,67);
Property property2 = new Property(24,"Boston Malvern",3,45,67);
Property property3 = new Property(24,"Boston Caulfield",3,45,67);
Property property4 = new Property(24,"Boston Caulfield",3,45,67);
Property property5 = new Property(24,"Boston Caulfield",3,45,67);
SimplePropertyRepositoryImpl sl;
sl = new SimplePropertyRepositoryImpl();
sl.addProperty(property1);
sl.addProperty(property2);
sl.addProperty(property3);
sl.addProperty(property4);
sl.addProperty(property5);
}
SimplePropertyRepositoryImpl有一个方法addProperty,我将属性对象添加到arraylist。
public class SimplePropertyRepositoryImpl implements PropertyRepository{
ArrayList<Property> properties;
public SimplePropertyRepositoryImpl(){
properties = new ArrayList<>();
}
@Override
public void addProperty(Property property) throws Exception {
properties.add(property);
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
它给了我一个错误:
Exception in thread "main" java.lang.UnsupportedOperationException: Not supported yet.
at fit5042.tutex.repository.SimplePropertyRepositoryImpl.addProperty(SimplePropertyRepositoryImpl.java:29)
at fit5042.tutex.RealEstateAgency.main(RealEstateAgency.java:47)
请帮助我理解为什么在我没有使用任何不可修改的设置时会出现此异常。
答案 0 :(得分:3)
删除throw
中的无条件addProperty
。像,
public void addProperty(Property property) throws Exception {
properties.add(property);
// throw new UnsupportedOperationException("Not supported yet.");
}
如果上述throw
未被注释,那么总是 throw
UnsupportedOperationException
。