首页 > 解决方案 > 列表列表继承解决方案

问题描述

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

我有一个父类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>并且还继承了所有的添加/删除/等方法。这可能吗?

更新

这是我根据 Konstantin Pozhidaev 的回答修改后的代码。(我将在弄清楚这一点后很快对其进行压缩)。

import java.util.ArrayList;

// CheckList.java
public abstract class CheckList <T extends ListItem> {
    String name;
    ArrayList<T> items;

    public String getName() { return this.name; };
    public ArrayList<T> 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().equals(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().equals(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().equals(name)) {
                items.get(i).setCompleted();
                return true; // target found
            }
        }
        return false; // cannot find the target
    }
}

// ShoppingList.java
public class ShoppingList extends CheckList<ShoppingListItem> {

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

// ListItem.java
public abstract class ListItem {
    String name;
    boolean completed;

    public String getName() { return this.name; }
    public boolean getStatus() { return this.completed; }

    public void setName(String newName) { this.name = newName; }
    public void setCompleted() { this.completed = true; }
}

// ShoppingListItem.java
public class ShoppingListItem extends ListItem {
    private String name;
    private boolean completed;

    public ShoppingListItem(String name) {
        System.out.println(name);
        this.name = name;
        System.out.println(this.name);
        this.completed = false;
    }
}

但是,我的代码破坏了我所有的旧 JUnit 案例。这是我的示例测试之一:

@Test public void testShoppingListAddItem() {
    User userUnderTest = new User("Shen");

    // groceries list items
    userUnderTest.createShoppingList("Groceries");
    ShoppingList groceries = userUnderTest.getShoppingList().get(0);
    groceries.addItem("Apple");
    groceries.addItem("Banana");
    ArrayList<ShoppingListItem> groceriesItems = groceries.getItems();

    // house renovations list items
    userUnderTest.createShoppingList("House Renovation");
    ShoppingList hr = userUnderTest.getShoppingList().get(1);
    hr.addItem("Paint");
    hr.addItem("Flooring");
    ArrayList<ShoppingListItem> hrItems = hr.getItems();

    // assertions
    assertEquals("the first item suppose to be Apple", 
        "Apple", groceriesItems.get(0).getName());
    assertEquals("the second item suppose to be Banana", 
        "Banana", groceriesItems.get(1).getName());

    assertEquals("the first item suppose to be Paint", 
        "Paint", hrItems.get(0).getName()); 
    assertEquals("the second iten suppose to be Flooring", 
        "Flooring", hrItems.get(1).getName()); 
}

错误输出:

> java.lang.AssertionError: the first item suppose to be Apple
> expected:<Apple> but was:<null>

我认为问题仍然在我的继承中,但我不知道在哪里。如果您有任何想法,请告诉我。

标签: javalistoopinheritancedesign-patterns

解决方案


您应该使用ArrayList<? extends ListItem>而不是ArrayList<ListItem>抽象类。

也使用equals字符串比较的方法。

更新

您的抽象类应如下所示:

abstract class CheckList<T extends ListItem> {
   ArrayList<T> items;
   ArrayList<T> getItems() { return this.items; }
...

实施

public class ShoppingList extends CheckList<ShoppingListItem> {

您应该确定您的泛型类以进行严格的类使用。

完整清单:

import java.util.ArrayList;

abstract class CheckList<T extends ListItem> {
    String name;
    ArrayList<T> items;

    String getName() { return this.name; }
    ArrayList<T> 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().equals(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().equals(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().equals(name)) {
                items.get(i).setCompleted(true);
                return true; // target found
            }
        }
        return false; // cannot find the target
    }
}

class ListItem {
    private String name;
    private Boolean completed;

    public String getName() {
        return name;
    }

    public Boolean getCompleted() {
        return completed;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setCompleted(Boolean completed) {
        this.completed = completed;
    }
}

class ShoppingListItem extends ListItem {
    public ShoppingListItem(String name) {
        this.setName(name);
    }
}
public class ShoppingList extends CheckList<ShoppingListItem> {

    public ShoppingList (String name) {
        this.name = name;
        this.items = new ArrayList<>();
    }

    public void addItem(String name) {
        // add a new ShoppingListItem to items
        final ShoppingListItem item = new ShoppingListItem(name);
        this.items.add(item);
    }
}

推荐阅读