在Java中,实现类是否继承了接口常数?

发布于 2025-01-23 02:53:16 字数 1399 浏览 4 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长不大的小祸害 2025-01-30 02:53:16

这一切都归结为您如何定义“继承”一词的问题。

  • 类的实例不仅可以使用其自己的方法来调用,而且可以使用其任何超类中声明的所有方法来调用。您想称之为“继承”? Java社区这样做。

  • 类的实例不仅具有其声明的字段,而且在其任何超类中都声明了所有字段。您想称之为“继承”? Java社区这样做。

  • 一个子类隐含其超级类的所有特征,例如它所实现的。您想称之为“继承”? Java社区这样做。

  • 实现接口的类将在接口中声明所有方法。您想称之为“继承”? Java社区称此“实现接口”,而不是“继承”。 [我有些惊讶的是,Oracle书似乎也使用“继承”一词。]

  • [我有些惊讶的是, 要以接口名称前缀常数。您想称之为“继承”? Java社区没有,这只是速记符号。

  • 当导入导入java.aw.*之类的软件包时,您可以通过其简单名称访问类,而无需将它们带有其包装名称。您想称之为“继承”? Java社区没有,这只是速记符号。

当然,您可以用您喜欢的任何名字来调用这些(以及更多)语言功能,但如果引起误解,请不要感到惊讶。

It all boils down to the question how you define the term "inheritance".

  • An instance of a class can be called not only with its own methods, but also with all methods declared in any of its superclasses. Would you like to call that "inheritance"? The Java community does so.

  • An instance of a class does not only have the fields that it declared itself, but also all fields declared in any of its superclasses. Would you like to call that "inheritance"? The Java community does so.

  • A subclass implicitly has all the characteristics of its superclass, e.g. which interfaces it implements. Would you like to call that "inheritance"? The Java community does so.

  • A class implementing an interface will have all the methods declared in the interface. Would you like to call that "inheritance"? The Java community calls this "implementing an interface", not "inheritance". [I'm a bit surprised that the Oracle book seems to use the term "inheritance" here as well.]

  • In a class implementing an interface, you can access constants declared in the interface by their simple name, without the need to prefix the constant with the interface name. Would you like to call that "inheritance"? The Java community does not, it's just a shorthand notation.

  • When importing a package like import java.awt.*, you can then access classes by their simple name, without the need to prefix them with their package name. Would you like to call that "inheritance"? The Java community does not, it's just a shorthand notation.

Of course, you can call any of these (and many more) language features by any name you like, but don't be surprised if it causes misunderstandings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文