List<parent> List<child> 继承解决方案

所以我正在编写我的代码并尝试将抽象工厂设计模式应用到其中。这是情况。


我有一个父类CheckList和一个子类ShoppingList。除此之外,我还有从课堂ShoppingListItem延伸出来的ListItem课程。


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));

    }

}

我在这里遇到的问题是


ShoppingList.java:9: error: incompatible types:

ArrayList<ShoppingListItem> cannot be converted to ArrayList<ListItem>

                this.items = new ArrayList<ShoppingListItem>();

看起来Java不允许在 and 之间进行这种ArrayList<parent>继承ArrayList<child>。我想知道是否有任何解决方案?我试图使ShoppingList只有一个ArrayList<ShoppingListItem>并且还继承了所有的添加/删除/等方法。这可能吗?


猛跑小猪
浏览 183回答 2
2回答

jeck猫

您应该使用ArrayList<? extends ListItem>而不是ArrayList<ListItem>抽象类。也使用equals字符串比较的方法。更新您的抽象类应如下所示:abstract class CheckList<T extends ListItem> {&nbsp; &nbsp;ArrayList<T> items;&nbsp; &nbsp;ArrayList<T> getItems() { return this.items; }...实施public class ShoppingList extends CheckList<ShoppingListItem> {您应该确定您的泛型类以进行严格的类使用。完整清单:import java.util.ArrayList;abstract class CheckList<T extends ListItem> {&nbsp; &nbsp; String name;&nbsp; &nbsp; ArrayList<T> items;&nbsp; &nbsp; String getName() { return this.name; }&nbsp; &nbsp; ArrayList<T> getItems() { return this.items; }&nbsp; &nbsp; public String setName(String name) { return this.name = name; }&nbsp; &nbsp; public abstract void addItem(String name);&nbsp; &nbsp; public boolean editItem(String oldName, String newName) {&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < items.size(); i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (items.get(i).getName().equals(oldName)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; items.get(i).setName(newName);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true; // target found&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return false; // cannot find the target&nbsp; &nbsp; }&nbsp; &nbsp; public boolean deleteItem(String name) {&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < items.size(); i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (items.get(i).getName().equals(name)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; items.remove(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true; // target found&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return false; // cannot find the target&nbsp; &nbsp; }&nbsp; &nbsp; public boolean completeItem(String name) {&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < items.size(); i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (items.get(i).getName().equals(name)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; items.get(i).setCompleted(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true; // target found&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return false; // cannot find the target&nbsp; &nbsp; }}class ListItem {&nbsp; &nbsp; private String name;&nbsp; &nbsp; private Boolean completed;&nbsp; &nbsp; public String getName() {&nbsp; &nbsp; &nbsp; &nbsp; return name;&nbsp; &nbsp; }&nbsp; &nbsp; public Boolean getCompleted() {&nbsp; &nbsp; &nbsp; &nbsp; return completed;&nbsp; &nbsp; }&nbsp; &nbsp; public void setName(String name) {&nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; }&nbsp; &nbsp; public void setCompleted(Boolean completed) {&nbsp; &nbsp; &nbsp; &nbsp; this.completed = completed;&nbsp; &nbsp; }}class ShoppingListItem extends ListItem {&nbsp; &nbsp; public ShoppingListItem(String name) {&nbsp; &nbsp; &nbsp; &nbsp; this.setName(name);&nbsp; &nbsp; }}public class ShoppingList extends CheckList<ShoppingListItem> {&nbsp; &nbsp; public ShoppingList (String name) {&nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; &nbsp; &nbsp; this.items = new ArrayList<>();&nbsp; &nbsp; }&nbsp; &nbsp; public void addItem(String name) {&nbsp; &nbsp; &nbsp; &nbsp; // add a new ShoppingListItem to items&nbsp; &nbsp; &nbsp; &nbsp; final ShoppingListItem item = new ShoppingListItem(name);&nbsp; &nbsp; &nbsp; &nbsp; this.items.add(item);&nbsp; &nbsp; }}

慕后森

GenericClass<Parent>和之间没有继承关系GenericClass<Child>,但是有一个针对您的情况的解决方案,通配符:ArrayList<? extends ListItem> items = new ArrayList<>(); //list with wildcard您将能够将任何扩展的内容ListItem放入其中。还可以考虑使用 foreach 循环甚至更好的 lambda 表达式使循环更紧凑。例如你的删除方法:public boolean deleteItem(String name) {&nbsp; &nbsp; boolean removed = false;&nbsp; &nbsp; items.removeIf(item -> {&nbsp; &nbsp; &nbsp; &nbsp;item.getName().equals(name);&nbsp; &nbsp; &nbsp; &nbsp;removed = true;&nbsp; &nbsp; });&nbsp; &nbsp; return removed;}顺便说一下,您应该将字符串与equals方法进行比较。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java