核心 Java 库中继承的最佳示例是什么?
我正在准备面试,并且认为如果我对“用示例解释 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为每个类都有
Object< /code>
作为超类是一个很好的起点。
I think the fact that every class has
Object
as a superclass is a good starting point.Throwable 类是所有异常和错误的超类。
当然,Throwable 是从 Object 扩展而来的。
class Throwable is super class of all the Exceptions and Errors.
And of course Throwable extends from Object.
我不知道您到底想知道什么,但这里有一个继承示例:
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:
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.
集合框架。它很好,因为它包含多种继承:
列表
扩展集合
,一个例子接口继承AbstractList
实现了List
,这是实现而不是继承,但是是一个相关的思想ArrayList
extendsAbstractList
,用于实现重用的类继承的示例我找不到的示例是定义子类型关系的目的。但是,如果稍微横向移动一下,java.util 包还包含:
GregorianCalendar
extendsCalendar
The collections framework. It's good because it contains several kinds of inheritance:
List
extendsCollection
, an example of interface inheritanceAbstractList
implementsList
, which is implementation rather than inheritance, but is a related ideaArrayList
extendsAbstractList
, an example of class inheritance for implementation reuseWhat 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
extendsCalendar
您还可以提及无法扩展的 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