简体   繁体   English

Java Enum不能扩展其他Enum,class可以

[英]Java Enum can't extends other Enum, class can

User from Stackoverflow asked this question, unfortunately doesn't have answer which can explain situation. Stackoverflow 的用户提出了这个问题,遗憾的是没有可以解释情况的答案。

Why enum can't extend another class while all other classes can 为什么枚举不能扩展另一个 class 而所有其他类都可以

I have the same question.我也有同样的问题。 Java also tells that it is multyple extends and it is not supports. Java 还告诉它是多扩展并且不支持。 Java documentation. Java 文档。

But this part confusing me.但这部分让我很困惑。 If any class by default extends Object class(Main class in Java), and any new class can extend another class (except Object class), but any new Enum can't? If any class by default extends Object class(Main class in Java), and any new class can extend another class (except Object class), but any new Enum can't? Why?为什么?

Any Enum extends Enum class by default and for special for enum can't extend more classes, and if say it is program way, will be more understandable.任何 Enum 默认扩展 Enum class 并且对于特殊的 enum 不能扩展更多的类,如果说它是程序方式,会更容易理解。 But in documentation was writen can't extends because it is multyple classes.但是在文档中写不能扩展,因为它是多个类。 Maybe Documentation isn't correct?也许文档不正确? If someone can explain this part, please explain this.如果有人可以解释这部分,请解释一下。

Thank you!谢谢!

As the comments on the question you linked to explain, all classes do not extend Object.正如对您链接的问题的评论所解释的那样,所有类都不会扩展 Object。

A class: class:

class Foo { }

implicitly extends Object , but a class:隐式扩展Object ,但扩展为 class:

class Bar extends Foo { }

Just extends Foo .只是扩展Foo

It is also a descendant of Object via Foo .它也是Object通过Foo的后代。

So an enum implicitly extends Enum , and can't extend anything else.所以一个 enum 隐式地扩展了Enum ,并且不能扩展其他任何东西。

Enums are final classes, so nothing can extend them.枚举是最终类,所以没有什么可以扩展它们。

From the documentation you have linked:从您链接的文档中:

Note: All enums implicitly extend java.lang.Enum.注意:所有枚举都隐式扩展 java.lang.Enum。 Because a class can only extend one parent (see Declaring Classes), the Java language does not support multiple inheritance of state (see Multiple Inheritance of State, Implementation, and Type), and therefore an enum cannot extend anything else. Because a class can only extend one parent (see Declaring Classes), the Java language does not support multiple inheritance of state (see Multiple Inheritance of State, Implementation, and Type), and therefore an enum cannot extend anything else.

Basically Enum has complier magic built in, that actually has the complier extend the Enum class from the java.lang.Enum .基本上Enum内置了编译器魔法,实际上编译器从 java.lang.Enum 扩展了 Enum java.lang.Enum Java doesn't support multiple inheritance of classes and hence you cant have extended Enums. Java 不支持多个 inheritance 类,因此您不能扩展枚举。

See this from the spec jls-8.9 as well.也可以从规范 jls-8.9中看到这一点。

An enum type is implicitly final unless it contains at least one enum constant that has a class body.枚举类型是隐式最终的,除非它包含至少一个具有 class 主体的枚举常量。

It is a compile-time error to explicitly declare an enum type to be final.将枚举类型显式声明为最终类型是编译时错误。

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

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