java 循环继承使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13464055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cyclic inheritance uses
提问by Valtteri
So if I write the following code in Java:
因此,如果我用 Java 编写以下代码:
public class A extends A{
public static void main(String[] args){
}
}
This gives a compiler error message cyclic inheritance involving A.
这给出了涉及 A 的编译器错误消息循环继承。
The same happens if I write two classes A and B and A inherits B and B inherits A.
如果我编写两个类 A 和 B,并且 A 继承 B 而 B 继承 A,也会发生同样的情况。
This makes sense to me, as it is quite hard to imagine how this would be possible anyway.
这对我来说很有意义,因为很难想象无论如何这是可能的。
I then asked about this from one of the professors at my uni. He said there are languages where this is possible and he lamented how this is not possible in Java and that he had done some projects where he had used cyclic inheritance and so on, but I couldn't really understand any of it. He also mentioned he had had problems where he would have liked to use cyclic inheritance.
然后我向我大学的一位教授询问了这个问题。他说有些语言可以做到这一点,他感叹这在 Java 中是不可能的,并且他做过一些使用循环继承等的项目,但我无法真正理解其中的任何内容。他还提到他在使用循环继承时遇到了问题。
Can you educate me on the possible uses of this strange phenomena of cyclic inheritance? When is it possible and how? Are there problems where this could be useful?
你能告诉我这种奇怪的循环继承现象的可能用途吗?什么时候可能?如何可能?是否存在可能有用的问题?
采纳答案by alexfernandez
I dug up this interesting reference: basically it says that cyclic inheritance is valid as long as there are no repeated fields, as the lookup for any field just needs to traverse one loop of the cycle to find out a meaning. If a field is repeated then none of the two definitions is more valid than the other and apparently there would be a problem.
我挖出了这个有趣的参考资料:基本上它说只要没有重复的字段,循环继承就是有效的,因为查找任何字段只需要遍历循环的一个循环即可找出含义。如果一个字段重复,那么这两个定义中没有一个比另一个更有效,显然会有问题。
So suppose that you want to define a person as a human and as a voter, and set different attributes for each. In pseudo-code:
因此,假设您要将一个人定义为人类和选民,并为每个人设置不同的属性。在伪代码中:
class Person extends Human:
String name;
class Human extends Voter:
String language;
class Voter extends Person:
Country residence;
Now you can address different aspects of an individual without having to define a hierarchy, and you might instantiate different people as a Person (with name), a Human (that speaks a language) or a Voter (in a particular country). No aspect is more important than the other.
现在,您可以解决个人的不同方面而无需定义层次结构,并且您可以将不同的人实例化为人(有姓名)、人(会说一种语言)或选民(在特定国家/地区)。没有哪个方面比另一个更重要。
While interesting, I don't think it is practical to use outside of research projects. Imagine having constructors for all classes that pass parameters to the super() constructors -- it would be easy to mess up the whole construct.
虽然有趣,但我认为在研究项目之外使用是不切实际的。想象一下,所有将参数传递给 super() 构造函数的类都有构造函数——很容易弄乱整个构造。
Update: the given pseudocode does not compile when translated to Java 8, and apparently under any language (except Cecil as shown by the link given above). It seems that nobody found any valid uses and therefore disallowed cyclic inheritance. This does not mean that the concept is inherently impossible; just that the practical uses do not justify the effort implementing the special case.
更新:给定的伪代码在翻译为 Java 8 时无法编译,显然在任何语言下都无法编译(除了上面给出的链接所示的 Cecil)。似乎没有人发现任何有效的用途,因此禁止循环继承。这并不意味着这个概念本质上是不可能的;只是实际用途并不能证明实现特殊情况的努力是合理的。
回答by user3243242
I disagree with the accepted answer that states that their code will not throw an error. I am running Java 8 and compiled the following code:
我不同意接受的答案,即他们的代码不会引发错误。我正在运行 Java 8 并编译了以下代码:
class Person extends Human:
String name;
class Human extends Voter:
String language;
class Voter extends Person:
String residence;
and I recieved an error stating "error: cyclic inheritance involving Person."
我收到一条错误消息,指出“错误:涉及 Person 的循环继承”。
Therefore, in Java you can not have cyclic inheritance.
因此,在 Java 中你不能有循环继承。
回答by Anshul Samaiyar
Cyclic inheritanceis of no use and moreover let us see why logically it is not allowed.
循环继承没有用,而且让我们看看为什么它在逻辑上是不允许的。
I would like to start with Object
class, if a class doesn't extend any class it extends Object
class (this is true), and if a class extends any other class indirectly extends the Object
class.
我想从Object
类开始,如果一个类不扩展任何类它扩展Object
类(这是真的),如果一个类扩展任何其他类间接扩展Object
该类。
Example:
例子:
class B
{
..//
}
class A extends B
{
...//
}
class A
extends Object
, because B
class extends Object
class.
类A
扩展Object
,因为B
类扩展了Object
类。
So when we do Cyclic Inheritance, it never extended the Object class.
所以当我们做循环继承时,它从不扩展 Object 类。
Example:
例子:
class A extends A
{
}
I hope I was clear, so and a class that cannot extend Object
class that is not possible in Java.
我希望我很清楚,所以和一个不能扩展Object
在 Java 中不可能的类的类。
回答by shaunw
I could only see it possible if the classes were on the same level of hierarchy. Think of the class hierarchy as a tree, which it is. Java looks for the hierarchy to be at least one level above or more. In some languages, you would be able to inherit characteristics from a class on the same level as the class you are using.
我只能看到如果这些类在同一层次结构上是可能的。将类层次结构视为一棵树,它就是这样。Java 查找层次结构至少比上面一层或更多。在某些语言中,您可以从与您使用的类处于同一级别的类继承特征。
回答by Hyman
I don't get the sense of cyclic inheritance. I don't know why your professor thinks it's useful in anycase mind that the inheritance reation that is called IS-Arelationship states that if B is a subclass of A then B IS-A A in the sense that everywhere an A is required then a B can be used without problem (Liskov substitution principle).
我没有得到循环继承的感觉。我不知道为什么你的教授认为这在任何情况下都是有用的,记住,被称为IS-A关系的继承关系指出,如果 B 是 A 的子类,那么 B IS-A A,因为在任何地方都需要 A 那么a B 可以毫无问题地使用(Liskov 替换原则)。
Now, theoretically, if A is a subclass of B and B is a subclass of A then then both classes must have exactly the same outside interface. This because if you add a method to any of them the other one will inherit the same method automatically so you will have either to override it either to get the other implementation.
现在,理论上,如果 A 是 B 的子类,而 B 是 A 的子类,那么这两个类必须具有完全相同的外部接口。这是因为如果您向其中任何一个添加一个方法,另一个方法将自动继承相同的方法,因此您必须要么覆盖它,要么获取另一个实现。
In addition you will have many circumstanced in which odd side effects comes into play (think about method A.foo()
calling super.foo()
and B.foo()
calling super.foo()
. I don't see any practical reason because this should be allowed.
此外,您会遇到许多奇怪的副作用发挥作用的情况(想想方法A.foo()
调用super.foo()
和B.foo()
调用super.foo()
。我没有看到任何实际原因,因为这应该被允许。
Inheritance is intended as a tree in which every subclass specifies the behavior or the classes that are up in the tree, having two classes at the same level doesn't mean anything useful.
继承旨在作为一棵树,其中每个子类都指定树中的行为或类,具有同一级别的两个类并不意味着任何有用的东西。