So I am working on my code and trying to apply abstract factory design pattern into it. Here's the situation.
I have a parent class CheckList
and a child class ShoppingList
. Aside from this, I also have ShoppingListItem
class which extended from ListItem
class.
public abstract class CheckList {
String name;
ArrayList<ListItem> items;
public String getName() { return this.name; };
public ArrayList<ListItem> getItems() { return this.items; };
public String setName(String name) { return this.name = name; };
public abstract void addItem(String name);
public boolean editItem(String oldName, String newName) {
for (int i = 0; i < items.size(); i++)
{
if (items.get(i).getName() == oldName) {
items.get(i).setName(newName);
return true; // target found
}
}
return false; // cannot find the target
}
public boolean deleteItem(String name) {
for (int i = 0; i < items.size(); i++)
{
if (items.get(i).getName() == name) {
items.remove(i);
return true; // target found
}
}
return false; // cannot find the target
}
public boolean completeItem(String name) {
for (int i = 0; i < items.size(); i++)
{
if (items.get(i).getName() == name) {
items.get(i).setCompleted();
return true; // target found
}
}
return false; // cannot find the target
}
}
public class ShoppingList extends CheckList {
public ShoppingList (String name) {
this.name = name;
this.items = new ArrayList<ShoppingListItem>();
}
public void addItem(String name) {
// add a new ShoppingListItem to items
items.add(new ShoppingListItem(name));
}
}
The issue I have here is that
ShoppingList.java:9: error: incompatible types: ArrayList cannot be converted to ArrayList this.items = new ArrayList();
Looks like that Java does not allow this kind of inheritance between ArrayList<parent>
and ArrayList<child>
. I am wondering is there any solution for this? I am trying to make ShoppingList
only has a ArrayList<ShoppingListItem>
and also inheritanced all the add/delete/etc methods. Is that possible?
Aucun commentaire:
Enregistrer un commentaire