Java对象、继承和接口
在Java中,一个对象可以有多个不同的类吗?
如果是,继承和接口对其有何影响?
In Java, Can an object have several different classes ?
If yes , how should Inheritance and Interface influence to it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是一个很好提出的问题。任何对象都可以属于一个且仅有一个类(忽略像 int 这样的非对象基元。)另一方面,一个类可以具有任意数量的超类,因此您的类可以匹配任意类型的“is a”关系其他类别的数量。
在接口问题上,接口带来的只是一个“契约”——你承诺实现某些方法或具有某些属性。这有点类似于多重继承,但并不是真正的多重继承。同样,实现接口意味着该类具有另一个“is a”属性,但这并不意味着它完全引入了其他类。
It's not a very well-posed question. Any object can be of one and only one class (ignoring the non-object primitives like int.) On the other hand, a class may have an arbitrary number of superclasses, so your class can match the "is a" relationship of an artitrary number of other classes.
On the interface question, all an interface brings in is a "contract" -- you're making a promise to implement certain methods or have certain properties. This is somewhat similar to multiple inheritance but not really multiple inheritance. Again, implementing an interface means the class has another "is a" property, but it doesn't mean it's fully bringing in the other classes.
是的
Yes