核心 Java 库中继承的最佳示例是什么?

发布于 2024-12-11 13:05:00 字数 344 浏览 0 评论 0原文

我正在准备面试,并且认为如果我对“用示例解释 java 中的继承”之类的问题的回答是核心 java 类的真正实现,那就太好了。 有很多例子,例如动物层次结构或形状,但我认为代表真实情况并尝试回答问题会更明智 - 为什么这种实现适合这种情况。
这样你就表明你不仅拥有继承方面的知识,而且还拥有核心 Java :))) 那么你对此有何看法?

添加: 另一篇好文章: 继承的坏例子是什么在Java中? 但这篇文章中的问题与我的相反。

I'm preparing to interview and think that would be good if my answer for the question like "Explain inheritance in java with an example" will be real implementation from core java classes.
There are many examples like Animals hierarchic or Shape but I think it would be more wisdom represent real situation and try answer for question - why this implementation good for this situation.
With that You show that you have good knowledge not just inheritance but and core Java :)))
So what do you think about that??

Addition:
Good another article: What can be the bad example of inheritance in Java?
But in this article question is opposite to my.

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

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

发布评论

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

评论(5

朮生 2024-12-18 13:05:00

我认为每个类都有 Object< /code>作为超类是一个很好的起点。

I think the fact that every class has Object as a superclass is a good starting point.

寒江雪… 2024-12-18 13:05:00

Throwable 类是所有异常和错误的超类。
当然,Throwable 是从 Object 扩展而来的。

class Throwable is super class of all the Exceptions and Errors.
And of course Throwable extends from Object.

雨轻弹 2024-12-18 13:05:00

我不知道您到底想知道什么,但这里有一个继承示例:

Collection<T> extends Iterable <T>

Iterable 接口使实现它的每个数据结构都可以与增强的 for 循环一起使用成为可能。 Collection 接口是 Java 中大多数数据结构的超级接口。 Getters 可以返回一个 Collection 以使实现更加灵活。对于对象中的内部数据表示,您可以使用列表、队列等。在更改实现时,您不必担心使用该类的其他地方。

编辑

所以你想要讨论。

我认为,对于雇主来说,重要的是你会使用 Java,而不是你知道它在 API 背后是如何工作的。 API 只是使用标准的设计概念,没有什么特别的。作为一名专业程序员,你应该了解它们并知道在哪里使用它们,但不知道其他程序员在哪里使用它们。

I don't know what you exactly want to know but here is one inheritance example:

Collection<T> extends Iterable <T>

The Iterable interface makes it possible, that every data structure implementing it can be used with enhanced for loops. The Collection interface is the superinterface for most datastructures in Java. Getters can return a Collection to make an implementation more flexible. For internal data representation in the Object you could for example use Lists, Queues or others. You don't have to bother about other places you used the class when changing the implementation.

EDIT

So you want discussion.

I think, for an employer it is important, that you can use Java, not that you know how it works behind the API. The API just uses standart design concepts, not much really special. As a professional programmer, you should have understood them and know where to use them, but not know where they are used by other programmers.

猥琐帝 2024-12-18 13:05:00

集合框架。它很好,因为它包含多种继承:

  • 列表扩展集合,一个例子接口继承
  • AbstractList 实现了List,这是实现而不是继承,但是是一个相关的思想
  • ArrayList extends AbstractList,用于实现重用的类继承的示例

我找不到的示例是定义子类型关系的目的。但是,如果稍微横向移动一下,java.util 包还包含:

  • GregorianCalendar extends Calendar

The collections framework. It's good because it contains several kinds of inheritance:

  • List extends Collection, an example of interface inheritance
  • AbstractList implements List, which is implementation rather than inheritance, but is a related idea
  • ArrayList extends AbstractList, an example of class inheritance for implementation reuse

What i can't find an example of is class inheritance for the purpose of defining a subtype relationship. However, if you move sideways a little, the java.util package also contains:

  • GregorianCalendar extends Calendar
半暖夏伤 2024-12-18 13:05:00

您还可以提及无法扩展的 String 类,以表明您还知道如何在需要时停止继承

You can also mention the String class which can not be extended to show you know also how to stop inheritence if you need to

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