简体   繁体   English

Undestanding Java Inheritance:奇怪的行为

[英]Undestanding Java Inheritance: strange behaviour

I don't understand a strange behavior of class inheritance. 我不明白类继承的奇怪行为。

This is my parent class: 这是我的父类:

public class Cubetti implements Token{
   ...
    private int id=1;
   ...
    public Cubetti(int n) {
        numero = n;
    }
   ...
    public int getId() { return id; }
    public void setId( int idx) { id = idx; }
   ...
}

and this is the subclass: 这是子类:

public class RGC extends Cubetti{
    private int id=0;
   ...
    public RGC (int idx) { 
        super(0);
        id = idx; // STRANGE BEHAVIOUR !
    }
   ...
}

This is the test mainclass: 这是测试主类:

public static void main(String[] args) {

        RGC uno = new RGC(1);
        RGC due = new RGC(2);

        System.out.println(" uno Id is  " + uno.getId() + " due Id is" + due.getId());

 }

The output is 输出是

Uno Id is 1 and due Id is 1 Uno Id为1,到期Id为1

but if I use in the tagged line on the RGC subclass: 但如果我在RGC子类的标记行中使用:

....
// id = idx;
setId(idx);
....

The output is 输出是

Uno Id is 1 and due Id is 2 Uno Id为1,到期Id为2

Why? 为什么?

You have an id variable in both the Cubetti super-class and RGC sub-class. Cubetti超类和RGC子类中都有一个id变量。 Using the setter and getter updates/returns the id of the super-class, since those methods are defined in the super-class and are not overridden by the sub-class. 使用setter和getter更新/返回超类的id ,因为这些方法是在超类中定义的,并且不被子类覆盖。

Calling id = idx in the sub-class constructor modifies the sub-class's variable, since the sub-class's variable hides the super-class's variable, and even if it didn't hide it, you wouldn't be able to access it from the sub-class, since it's private. 在子类构造函数中调用id = idx会修改子类的变量,因为子类的变量隐藏了超类的变量,即使它没有隐藏它,你也无法从中访问它子类,因为它是私有的。

In Java :: 在Java ::
private properties are not inherited to the derived class. 私有属性不会继承到派生类。 So the Cubetti 's private property is not seen (directly accessible) in the scope of RGC 's class. 因此,在RGC班级的范围内, Cubetti的私有财产并未被看到(可直接访问)。 Cubetti 's id property is only accessible through the outside world using the get() & set() methods (if these method are declared public). Cubetti的id属性只能通过外部世界使用get()set()方法访问(如果这些方法被声明为public)。

In the 1st case where your output is following: 在第一种情况下,您的输出如下:
Uno Id is 1 and due Id is 1
The reason is that, the Cubetti 's getId() & setId() methods are inherited to the RGC class as they are public method of Cubetti class. 原因是, CubettigetId()setId()方法继承到RGC类,因为它们是Cubetti类的公共方法。 So whenever you call the get() method you get the value of the Cubetti 's id value, which is set to 1 . 因此,每当调用get()方法时,都会得到Cubetti的id值的值,该值设置为1 That is why you get the value 1 for both Uno & Due . 这就是为什么你得到UnoDue的值1Due

In the second case: 在第二种情况:
you place setId() in the RGC constructor. 你将setId()放在RGC构造函数中。 Here you basically set the id of the Cubetti 's ID. 在这里你基本上设置了Cubetti ID的id。 Thus calling the getId() the id of the Cubetti is being returned. 因此,返回调用getId()Cubettiid

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM