java中继承的概念
嗨,我是 Java 新手....从 Objective C(iPhone 背景)移过来,
我们都知道 在这里我们不能使用多重继承......
替代方法是接口......
我的问题是......通过接口继承有任何意义......因为我们必须在我们的函数中定义函数的代码类......这里唯一有帮助的部分是变量...... 它就像 Objective C 中的单个 .h 类...
我只讨论该接口的函数...为什么要在那里声明...只是为了节省 2 或三行...就是这样...
请告诉为什么没有多重继承(原因很简单......)。
我知道这可能是一个不好的问题,但是……
我有点黑暗,请帮助……
hi i am new to java....moved from objective c (iphone background)
all we know that
here we cannot use multiple inheritance....
alternative way is interface......
my question is...... does inheritance through interfaces make any sense...because we have to define the code of function in our class.......only helping part here is variables...
it is like a single .h class in objective c...
i am talking about only functions of that interface...why to declare there....just to save 2 or three lines...that's it..
please tell why there is no multiple inheritance(simple reason...).
i know this may be a bad question to ask but.........
i am in little bit of dark please help.....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我喜欢斯蒂芬·施密特的回答。他对此解释得很清楚:
http://codemonkeyism.com/java-interview-questions-mutliple-inheritance/
(链接失效)。I love this answer from Stephan Schmidt. He clearly explains about this:
http://codemonkeyism.com/java-interview-questions-mutliple-inheritance/
(link defunct).我想在之前的回答中补充一些内容。
正如您从 Tilsan The Fighter 给出的深入解释中了解到的那样,Java 不支持多重继承。
或者,您应该使用委托设计模式之一。我知道有时代码看起来比使用多重继承完成的代码更冗长,但这是我们必须付出的代价。
您问了一个问题,为什么需要创建实现多个接口的类?我想问你一个问题,为什么你需要界面?我认为答案是,这允许您将接口(契约)与具体实现分离,使模块独立,简化重构并使程序更加灵活:只有当具体实现被接口隐藏时,您才能切换实现。
如果您的类可以在不同的上下文中用作 A 和 B(其中 A 和 B 是接口),则该类应该实现 2 个接口。
示例: 假设您有一个仅声明一个方法的业务接口 Foo
现在您创建了几个实现 Foo 的类 A 和 B:
现在您想要创建 Foo 的集合:
使用该集合的代码不知道任何关于具体实现,但它不会打扰它调用 foo()。
现在您希望对此集合进行排序。其中一种方法是实现另一个接口 Comparable:
完成后,您可以说:
并且集合将根据compareTo() 中定义的规则进行排序。
现在您可能希望序列化 A 和 B 的实例。为此,您必须实现另一个 Serialized 接口。幸运的是 Serialized 是一个特殊的接口,没有声明任何方法。 JVM 知道如何序列化和反序列化作为 Serialized 实例的对象。
B级看起来是一样的。
现在我们可以说:
并将我们的对象存储在文件
/tmp/foo.dat
中。我们可以稍后读取这些对象。我试图说明为什么我们需要实现多个接口的类:每个接口都赋予该类其行为。 Comparable 允许对此类实例的集合进行排序。可序列化允许序列化等。
I would like to add something to the previous answer.
As you understood from the deep explanation given by Tilsan The Fighter multiple inheritance is not supported by Java.
Alternatively you should use one of delegation design patterns. I know that sometimes the code looks more verbose than code done using multiple inheritance but this is the price we have to pay.
You asked a question why do you need to create class that implements more than one interface? I'd ask you a question why do you need interface at all? I think that the answer is that this allows you to separate interface (contract) from concrete implementation, make modules independent, simplify re-factoring and make programs more flexible: you can switch implementations only if the concrete implementation is hidden by interface.
If your class can be used in different contexts as A and as B (where A and B are interfaces) the class should implement 2 interfaces.
Example: Let's assume that you have a business interface Foo that declares only one method
Now you create a couple of classes A and B that implement Foo:
Now you would like to create collection of Foo:
Code that uses this collection does not know anything about the concrete implementation but it does not bother it to invoke foo().
Now you wish to sort this collection. One of the ways is to implement yet another interface Comparable:
Once you are done you can say:
And the collection will be sorted according to the rule defined in compareTo().
Now you probably wish to serialize the instances of A and B. To do this you have to implement yet another interface Serializable. Fortunately Serializable is a special interface that does not declare any method. JVM knows to serialize and desirialize objects that are instances of Serializable.
Class B looks the same.
Now we can say:
and store our objects in file
/tmp/foo.dat
. We can read the objects later.I tried to show why do we need classes that implement several interfaces: each interface gives the class its behavior. Comparable allows sorting collection of such instances. Serializable allows serialization etc.