第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

List<parent> List<child> 繼承解決方案

List<parent> List<child> 繼承解決方案

猛跑小豬 2022-06-23 17:05:56
所以我正在編寫(xiě)我的代碼并嘗試將抽象工廠設(shè)計(jì)模式應(yīng)用到其中。這是情況。我有一個(gè)父類(lèi)CheckList和一個(gè)子類(lèi)ShoppingList。除此之外,我還有從課堂ShoppingListItem延伸出來(lái)的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));    }}我在這里遇到的問(wèn)題是ShoppingList.java:9: error: incompatible types:ArrayList<ShoppingListItem> cannot be converted to ArrayList<ListItem>                this.items = new ArrayList<ShoppingListItem>();看起來(lái)Java不允許在 and 之間進(jìn)行這種ArrayList<parent>繼承ArrayList<child>。我想知道是否有任何解決方案?我試圖使ShoppingList只有一個(gè)ArrayList<ShoppingListItem>并且還繼承了所有的添加/刪除/等方法。這可能嗎?
查看完整描述

2 回答

?
jeck貓

TA貢獻(xiàn)1909條經(jīng)驗(yàn) 獲得超7個(gè)贊

您應(yīng)該使用ArrayList<? extends ListItem>而不是ArrayList<ListItem>抽象類(lèi)。


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


更新


您的抽象類(lèi)應(yīng)如下所示:


abstract class CheckList<T extends ListItem> {

   ArrayList<T> items;

   ArrayList<T> getItems() { return this.items; }

...

實(shí)施


public class ShoppingList extends CheckList<ShoppingListItem> {

您應(yīng)該確定您的泛型類(lèi)以進(jìn)行嚴(yán)格的類(lèi)使用。


完整清單:


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

    }

}


查看完整回答
反對(duì) 回復(fù) 2022-06-23
?
慕后森

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊

GenericClass<Parent>和之間沒(méi)有繼承關(guān)系GenericClass<Child>,但是有一個(gè)針對(duì)您的情況的解決方案,通配符:


ArrayList<? extends ListItem> items = new ArrayList<>(); //list with wildcard

您將能夠?qū)⑷魏螖U(kuò)展的內(nèi)容ListItem放入其中。


還可以考慮使用 foreach 循環(huán)甚至更好的 lambda 表達(dá)式使循環(huán)更緊湊。例如你的刪除方法:


public boolean deleteItem(String name) {

    boolean removed = false;

    items.removeIf(item -> {

       item.getName().equals(name);

       removed = true;

    });

    return removed;

}

順便說(shuō)一下,您應(yīng)該將字符串與equals方法進(jìn)行比較。


查看完整回答
反對(duì) 回復(fù) 2022-06-23
  • 2 回答
  • 0 關(guān)注
  • 213 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)