简体   繁体   English

Java泛型编译错误-不了解该错误

[英]Java Generics Compilation Error - Do not understand the error

I know many ask such problems, but I really do not get this one. 我知道很多人都在问这样的问题,但是我真的没有这个问题。

(If you need more code, please tell) (如果您需要更多代码,请告知)

public class QuestionManager<Question extends Component & IQuestion<? extends IAnswerStorage>>

public class AccountingQuestionManager<Question extends Component & IQuestion<? extends IAnswerStorage>> extends QuestionManager<Question>

public interface IQuestion<DataStorage extends IAnswerStorage>

IAnswerStorage is an empty interface IAnswerStorage是一个空接口

These are my 3 generic classes I need for explaining. 这些是我需要解释的3个泛型类。 In a QuestionManager the questions need to be questions ( IQuestions ) [with storage specified] QuestionManager ,问题必须是问题( IQuestions )[指定存储空间]

I now tried the following in the hope that it allows any questions, which are components as well. 我现在尝试以下操作,希望它允许任何问题,这些问题也是组成部分。

QuestionManager<? extends Component & IQuestion<? extends IAnswerStorage>> manager = new AccountingQuestionManager<>(
            "Test test", this);

I also have a function to add questions to a pool of questions: 我还有一个向问题池中添加问题的功能:

manager.addQuestion(question);

But I get the 2 following errors: 但是我收到以下两个错误:

Incorrect number of arguments for type QuestionManager<Question>; it cannot be parameterized with arguments <? extends Component, IQuestion<? extends IAnswerStorage>>
Syntax error on token "&", , expected

Thanks so much for your help. 非常感谢你的帮助。 Let me know if you need more. 让我知道您是否需要更多。

In this expression 在这个表达中

public class QuestionManager<Question extends Component & IQuestion<? extends IAnswerStorage>>

you are declaring a type parameter named Question which is a subtype of Component and of IQuestion<? extends IAnswerStorage> 您正在声明一个名为Question的类型参数 ,它是ComponentIQuestion<? extends IAnswerStorage>的子类型IQuestion<? extends IAnswerStorage> IQuestion<? extends IAnswerStorage> . IQuestion<? extends IAnswerStorage> That is, the type parameter Question has multiple bounds. 也就是说,类型参数Question具有多个界限。

In this expression 在这个表达中

QuestionManager<? extends Component & IQuestion<? extends IAnswerStorage>> manager = new AccountingQuestionManager<>("Test test", this);

what you are attempting to do is declare a type argument for the QuestionManager type. 您要尝试做的是为QuestionManager类型声明一个类型参数 A type argument cannot be declared to have multiple bounds. 类型参数不能声明为具有多个边界。 A type argument, by definition, already is a type with whatever bounds it has. 根据定义,类型实参已经是具有任何边界的类型。 You can't redefine it in the expression. 您不能在表达式中重新定义它。

Here are the syntax rules for type arguments. 以下是类型参数的语法规则。

You may use the generic & in a class declaration, but not in variable assignment. 您可以在声明中使用通用& ,但不能在变量分配中使用。 You can safely do 你可以放心地做

QuestionManager<?> manager = //...

because it is guaranteed in the specification of the type parameter Question that it will already fit ? extends Component & IQuestion<? extends IAnswerStorage> 因为它在类型参数的规范保障Question ,它已经将适合? extends Component & IQuestion<? extends IAnswerStorage> ? extends Component & IQuestion<? extends IAnswerStorage> ? extends Component & IQuestion<? extends IAnswerStorage> . ? extends Component & IQuestion<? extends IAnswerStorage>

So that is the way I was able to solve my specific problem (which I would not be able to, if I would be less generic) QuestionManager: 因此,这就是我能够解决自己特定问题的方式(如果我的通用性较差,我将无法解决)QuestionManager:

public class QuestionManager<Question extends IQuestion<? extends AnswerStorage>>

extends ExtBorderLayout 扩展ExtBorderLayout

And the "addQuestion" method looks like the following 而“ addQuestion”方法如下所示

public <QuestionComponent extends IQuestion<? extends AnswerStorage> & Component> void addQuestion(
        QuestionComponent question)

So, making the method generic did the job for me. 因此,使方法通用可以为我完成工作。

Thanks for your help! 谢谢你的帮助!

You cannot use '&' in the generic part when declaring a variable. 声明变量时,不能在通用部分中使用“&”。 It only works when declaring a generic class. 它仅在声明泛型类时起作用。 When declaring a variable/field that uses a generic class, always provide concrete classes/interfaces for the generic parameters. 声明使用泛型类的变量/字段时,请始终为泛型参数提供具体的类/接口。 eg, QuestionManager<Question> 例如, QuestionManager<Question>

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

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