简体   繁体   English

使用工厂方法创建泛型

[英]Using Factory Method to Create Generics

I have an abstract class (Candy) with a generic collection (Flavors). 我有一个带有通用集合(Flavors)的抽象类(Candy)。 Candy has a factory method to produce concrete instances of itself. Candy具有一种工厂方法来产生其自身的具体实例。 It also has methods to get an instance of the generic flavor appropriate to the concrete candy and to add the flavor to its collection. 它还具有获取适合于具体糖果的通用风味实例并将其添加到其集合中的方法。

I know the getter is working, because if I cast the flavor from the CandyStore, the methods unique to the concrete flavor work fine. 我知道吸气剂正在工作,因为如果我从CandyStore浇铸调味料,则具体调味料特有的方法可以正常工作。 But the very last line, the addFlavor(flavor), errs (Eclipse) on me. 但是最后一行,addFlavor(flavor)对我来说是错误的(Eclipse)。 The error is: "The method addFlavor(capture#5-of ? extends IFlavor) in the type ICandy is not applicable for the arguments (IFlavor)." 错误为:“类型为ICandy的方法addFlavor(capture#5-of?扩展IFlavor)不适用于自变量(IFlavor)。” Can anyone explain what is going on? 谁能解释发生了什么?

Interface: 接口:

public interface ICandy <Flavor extends IFlavor> {
    public Flavor getFlavorForCandy();
    public void addFlavor(Flavor flavor);
}

Abstract Class: 抽象类:

public abstract class AbstractCandy<Flavor extends IFlavor> implements ICandy<Flavor> {
    public static ICandy<? extends IFlavor> buildCandy(String flavor){
        if(flavor.equals("Jolly Rancher")
            return new JolRanchCandy();
    }
    public Flavor getFlavorForCandy() {
        return (Flavor) new CandyFlavor();
    }
    public void addFlavor(Flavor flavor) {
        ... //implemented
    }
}

Concrete Class: 具体类别:

public class JolRanchCandy extends AbstractCandy<JolRanchFlavor> {
    ... //implemented
}

Used By: 使用者:

public class CandyStore {
    private ICandy<? extends IFlavor> candy;
    private IFlavor flavor;
    public void createCandy() {
        candy = AbstractCandy.buildCandy("Jolly Rancher");
        flavor = candy.getFlavorForCandy(); //returns a JolRanchFlavor
        flavor.setName("Apple");            //etc for creating flavor
        candy.addFlavor(flavor);   //no luck
    }
}

Edit: For clarity, JolRanchFlavor extends CandyFlavor implements IJolRanchFlavor and CandyFlavor implements IFlavor . 编辑:为了清楚起见, JolRanchFlavor extends CandyFlavor implements IJolRanchFlavorCandyFlavor implements IFlavor

Try this... 尝试这个...

public <T extends IFlavor> void createCandy() {
    ICandy<T> candy= (ICandy<T>) AbstractCandy.buildCandy("Jolly Rancher");
    T flavor= candy.getFlavorForCandy(); 
    flavor.setName("Apple");            
    candy.addFlavor(flavor);  
}

The problem is the declaration of private ICandy<? extends IFlavor> candy 问题是private ICandy<? extends IFlavor> candy的声明private ICandy<? extends IFlavor> candy private ICandy<? extends IFlavor> candy . private ICandy<? extends IFlavor> candy Since the type of the candy is unknown and therefore ? 由于糖果的类型是未知的,因此? the compiler doesn't know exactly what kind of IFlavor addFlavor should take. 编译器并不确切知道应该使用哪种IFlavor addFlavor You just need to define a generic holder for the IFlavor type so that it is preserved. 您只需要为IFlavor类型定义一个通用持有人,就可以保留它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM