1

我有一家生产餐点的餐厅。厨房的盘子是消费者。

class Food{}
class Bamboo extends Food {}

interface Kitchen {
    void build(List<? super Food> dessert);
}

abstract class Restaurant {
    Kitchen kitchen;

    public Restaurant(Kitchen kitchen) {
        this.kitchen = kitchen;
    }

    List<? extends Food> getMeals() {
        List<Food> food = new ArrayList<>();
        this.kitchen.build(food);
        return food;
    }
}

class PandaKitchen implements Kitchen{

    // Exact signature of the Kitchen
    @Override
    public void build(List<? super Food> plate) {
        // the List IS a consumer of bamboos
        plate.add(new Bamboo());
    }
}

// Bamboo specialized restaurant
class OhPanda extends Restaurant {

    public OhPanda() {
        super(new PandaKitchen());
    }

    // Specialized signature of List<? extends Food>
    @Override
    List<Bamboo> getMeals() {
        List<? super Food> bamboos = new ArrayList<>();
        this.kitchen.build(bamboos);

        // Obviously here, there is no information of having only Bamboos
        return bamboos; // <==== FAIL

        //return (List<Bamboo>) bamboos; // would not compile
    }
}

在最后一行,我知道我的 OhPanda 餐厅只生产竹子。List<? super Food>在不创建/复制内存中的 ArrayList 的情况下转换我的最佳做法是什么?

更完整的 Gist 写在这里:https ://gist.github.com/nicolas-zozol/8c66352cbbad0ab67474a776cf007427

4

2 回答 2

1

我认为您错误地使用了下界通配符。通过在实现中不指定通配符的类,您可以使用它来解决您可能面临的上限通配符限制。我认为您不想与食物及其超类型一起工作。您只想使用 Food 及其衍生产品,您应该使用“? extends Food”找到解决方案,甚至摆脱通配符并使用 List< Food >

于 2017-07-20T10:55:00.183 回答
1

或者,也许您可​​以编写餐厅和厨房的打字版本?

package kitchen;

import java.util.ArrayList;
import java.util.List;

class Food{}
class Bamboo extends Food {}

interface Kitchen<F> {
    void build(List<F> dessert);
}

abstract class Restaurant<T> {
    protected Kitchen kitchen;

    Restaurant(Kitchen kitchen) {
        this.kitchen = kitchen;
    }

    List<T> getMeals() {
        List<T> food = new ArrayList<>();
        kitchen.build(food);
        return food;
    }
}

class PandaKitchen implements Kitchen<Bamboo>{

    @Override
    public void build(List<Bamboo> dessert)
    {
        dessert.add(new Bamboo());
    }
}

/** Bamboo specialized restaurant*/
class OhPanda extends Restaurant<Bamboo> {

    OhPanda() {
        super(new PandaKitchen());
    }

    @Override
    List<Bamboo> getMeals() {
        List<Bamboo> bamboos = new ArrayList<>();
        kitchen.build(bamboos);
        return bamboos;
    }
}
于 2017-07-20T12:26:23.787 回答