简体   繁体   English

使用泛型返回不止一种类型

[英]Using generics to return more than one type

I am currently trying to refactor some of my code and have stumbled into a feature I haven't used before in Java related to Generics. 我目前正在尝试重构我的一些代码,并且偶然发现了与Java相关的Java以前没有使用过的功能。

I am attempting to return an ArrayList<?> of a specific type based on the parameter passed into the method. 我试图根据传递给方法的参数返回特定类型的ArrayList<?> The Parameter accepts an Enumeration that specifies the desired type of ArrayList<T> To return, something like this, however I am getting errors from the method prototype: 该参数接受一个枚举,该枚举指定所需的ArrayList<T>类型以返回,类似这样,但是我从方法原型中得到了错误:

// Retrieves an ArrayList of questions available for a particular QuestionType
    public <T extends Question> ArrayList<T extends Question> getQuestions(QuestionType type) {
        switch (type) {
        case BASE_QUESTION:
            return mQuestions; // ArrayList<Question>

        case MULTIPLE_ANSWER_QUESTION:
        case MULTIPLE_CHOICE_QUESTION:
        case TRUE_FALSE_QUESTION:
            return mMultipleAnswerQuestions; // ArrayList<MultipleAnswerQuestion>

        case MATCHING_QUESTION:
            return mMatchingQuestions; // ArrayList<MatchingQuestion>

        case BLANK_QUESTION:
            return mBlankQuestions; // ArrayList<BlankQuestion>

        default:
            // TODO Perfect place to throw an exception
            Log.d(TAG, "Exception: Provided a non-existant QuestionType");
        }
    }

Additional Information: This method exists within a custom object that contains multiple ArrayList of Questions organised by their type. 附加信息:此方法存在于自定义对象中,该对象包含按问题类型组织的多个问题ArrayList。 This method does not Exist within the SuperClass of the Question type. 在Question类型的超类中不存在此方法。

// CLASS HIERARCHY
// SuperClass
Question

// SubClasses of Question
MultipleAnswerQuestion
MultipleChoiceQuestion
TrueFalseQuestion
MatchingQuestion
BlankQuestion

Can some explain to me how to prototype a method that achieves this task? 有人可以向我解释如何为实现此任务的方法建立原型吗?

Thanks for your understanding and help. 感谢您的理解和帮助。

Change your method signature to: 将方法签名更改为:

public ArrayList<? extends Question> getQuestions(QuestionType type) 

On a side note, you might want to just create an EnumMap to map your lists instead of doing a switch every time. 附带一提,您可能只想创建一个EnumMap即可映射列表,而不是每次都进行switch

Those particular compilation errors you got were probably because the syntax error of declaring the bounds on T a second time prevented the compiler from recognizing ArrayList<T extends Question> as your return type. 您遇到的那些特殊编译错误可能是因为第二次声明T的界限的语法错误阻止了编译器将ArrayList<T extends Question>作为您的返回类型。 So if you either remove extends Question or change your return type to a bounded wildcard the compilation errors should go away. 因此,如果您删除extends Question或将返回类型更改为有界通配符,则编译错误应该会消失。

In generic methods, you only specify bounds on the type parameter once -- when declaring them at the beginning of the method signature. 在泛型方法中,只需在类型参数上指定一次边界-在方法签名的开始处声明边界。 In other words, just use ArrayList<T> as the return type instead of ArrayList<T extends Question> , because you already specified what the bounds were on T in the generic method. 换句话说,只需使用ArrayList<T>作为返回类型,而不是ArrayList<T extends Question> ,因为您已经在通用方法中指定了T的边界。

So you'd end up with something like 所以你最终会得到类似

public <T extends Question> ArrayList<T> getQuestions(QuestionType type) {
    ...
}

In addition, because you don't appear to actually use T in your method, it's probably a better idea to skip the generic method altogether and just use a bounded wildcard in your return value, like this: 另外,由于您似乎并未在方法中实际使用 T ,因此最好完全跳过通用方法,而仅在返回值中使用有界通配符,这是一个更好的主意,如下所示:

public ArrayList<? extends Question> getQuestions(QuestionType type) {
    ...
}

The correct prototype is public <T extends Question> ArrayList<T> getQuestions(QuestionType type) or public ArrayList<? extends Question> getQuestions(QuestionType type) 正确的原型是public <T extends Question> ArrayList<T> getQuestions(QuestionType type)public ArrayList<? extends Question> getQuestions(QuestionType type) public ArrayList<? extends Question> getQuestions(QuestionType type)

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

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