"Cannot subclass the final class" 错误,但 class 不是最终的
"Cannot subclass the final class" error, but the class is not final
这是我的代码:
package basic;
public abstract class Entity {}
package characters;
import basic.Entity;
public abstract class Character extends Entity {}
package player;
public class Player extends Character {}
我得到了
The type Player
cannot subclass the final class Character
.
但我检查了一百万次,但我还没有在我的项目中使用 final
除了 ONCE。给出了什么?
您正在扩展 java.lang.Character
(不需要导入,因为它来自 java.lang
)。
将 import characters.Character
插入您的 Player
代码。
For convenience, the Java compiler automatically imports two entire packages for each source file: (1) the java.lang package and (2) the current package (the package for the current file).
字符是 java.lang 的 class("char" 的包装 class)。
您必须在播放器 class
中导入 characters.Character
package player;
import characters.Character
public class Player extends Character {
}
字符是最终的 class,如 Java 文档中所定义:
public final class Character
extends Object
implements Serializable, Comparable<Character>
所以它不能被子class编辑。
您正在从这个 class 字符 隐式 导入中收到错误。 小心!.
在这种情况下,我强烈建议使用the fully qualified name of the Character
class in the extends
clause。
public class Player extends characters.Character {}
有经验的 Java 开发人员知道 java.lang.Character
is final
并且不能因此扩展。通过写class Player extends Character
,你可能会让他们不知所措。
Every compilation unit implicitly imports every public type name declared in the predefined package java.lang
, as if the declaration import java.lang.*;
appeared at the beginning of each compilation unit immediately after any package declaration. As a result, the names of all those types are available as simple names in every compilation unit.
Java 11 Specification > 7. Packages and Modules > 7.3. Compilation Units
当然,从标准 java.lang
包中选择一个不与 类 冲突的名称(如 Person
,或 GameCharacter
).
这是我的代码:
package basic;
public abstract class Entity {}
package characters;
import basic.Entity;
public abstract class Character extends Entity {}
package player;
public class Player extends Character {}
我得到了
The type
Player
cannot subclass thefinal class Character
.
但我检查了一百万次,但我还没有在我的项目中使用 final
除了 ONCE。给出了什么?
您正在扩展 java.lang.Character
(不需要导入,因为它来自 java.lang
)。
将 import characters.Character
插入您的 Player
代码。
For convenience, the Java compiler automatically imports two entire packages for each source file: (1) the java.lang package and (2) the current package (the package for the current file).
字符是 java.lang 的 class("char" 的包装 class)。 您必须在播放器 class
中导入 characters.Characterpackage player;
import characters.Character
public class Player extends Character {
}
字符是最终的 class,如 Java 文档中所定义:
public final class Character
extends Object
implements Serializable, Comparable<Character>
所以它不能被子class编辑。
您正在从这个 class 字符 隐式 导入中收到错误。 小心!.
在这种情况下,我强烈建议使用the fully qualified name of the Character
class in the extends
clause。
public class Player extends characters.Character {}
有经验的 Java 开发人员知道 java.lang.Character
is final
并且不能因此扩展。通过写class Player extends Character
,你可能会让他们不知所措。
Every compilation unit implicitly imports every public type name declared in the predefined package
java.lang
, as if the declarationimport java.lang.*;
appeared at the beginning of each compilation unit immediately after any package declaration. As a result, the names of all those types are available as simple names in every compilation unit.Java 11 Specification > 7. Packages and Modules > 7.3. Compilation Units
当然,从标准 java.lang
包中选择一个不与 类 冲突的名称(如 Person
,或 GameCharacter
).