我正在通过JSP建立一个购物网站.我有一个名为ShoppingCart的Java对象和一个名为Item的对象.在ShoppingCart中,有一个包含Item对象的向量.这个想法是当我调用addItem()方法时,我使用:
cart.addItem(name, image, price, details);
确保已声明ShoppingCart:-
ShoppingCart cart = new ShoppingCart("file_path_to_file");
并且addItem的内容是:
public void addItem(String name, String image, String price, String detail) throws IOException
{
items.add(new Item(name, image, price, detail));
this.saveMe();
}
其中项目是向量.这绝对好用.但是,我现在创建了一个名为clear的新方法:
public void clear() throws IOException
{
items.clear();
this.saveMe();
}
saveMe方法只是保存到一个对象文件:
private void saveMe() throws IOException
{
FileOutputStream fos = new FileOutputStream(this.filename);
ObjectOutputStream oos = new ObjectOutputStream(fos)
oos.writeObject(this.items);
oos.close();
}
当我使用以下方法调用clear方法时:
cart.clear();
我收到错误消息:
An error occurred at line: 108 in the jsp file: /project/cart.jsp
The method clear() is undefined for the type ShoppingCart
任何人都可以提出任何想法来解决这个问题吗?