首页 > java > 可以`public <T extends Number> T方法(Integer x){...}`输入一个扩展Integer的类型?

可以`public <T extends Number> T方法(Integer x){...}`输入一个扩展Integer的类型? (Can `public <T extends Number> T method( Integer x ) {…}` take input of a type that ext)

问题

我对我的练习考试中的一个问题感到困惑。

在Java中,Integer类扩展了基本抽象类Number。现在,假设 我们有一个带签名的方法如下:

public <T extends Number> T method( Integer x ) {...}

对于以下各项,将“can”或“can not”写入空白以正确完成 该声明。

方法______接受扩展Integer的类型的任何输入

我很困惑,因为当我尝试创建我自己的扩展Integer的类时,IntelliJ告诉我,“不能从final'java.lang.Integer'继承”。

如果我不能从Integer类继承,那么我如何首先提供“扩展Integer”的输入?

解决方法

这个说法非常糟糕。我希望它是这样写的,因为混淆了学生,而不是因为他们忘记了原始类型的包装是final类。

所以,回答你的问题,答案是“ 不能 ”。
但如果可以,请与您的教授或考官澄清这一点。

问题

I am confused by one of the questions on my practice exam.

In Java, the Integer class extends the base abstract class Number. Now, suppose we have a method with a signature as follows:

public <T extends Number> T method( Integer x ) {...}

For each of the following, write “can” or “cannot” into the blanks to correctly complete the statement.

The method ______ take any input of a type that extends Integer

I am confused because when I try to make my own class that extends Integer, IntelliJ tells me, "Cannot inherit from final 'java.lang.Integer'".

If I can't inherit from the Integer class, then how would I give input that "extends Integer" in the first place?

解决方法

That statement is pretty badly formulated. I hope it was written like that for confusing the student, and not because they forgot primitive types' wrappers are final classes.

So, to answer your question, the answer would be "cannot".
But if you can, clarify this point with your professor or examiner.

相似信息