简体   繁体   English

对Java中子类的超类引用

[英]Super class reference to sub class in java

I was trying to reference child class object using parent class. 我试图使用父类引用子类对象。 According to Complete Reference Java, parent class can refer to the child class but it can access only those fields that is already declared in parent class (complete reference java 8th edition, page no:166, second last paragraph). 根据完全参考Java,父类可以引用子类,但是它只能访问父类中已经声明的那些字段(完全参考Java第8版,页面编号:166,倒数第二段)。

According to complete reference 据完整参考
It is important to understand that it is the type of the reference variable—not the type of the object that it refers to—that determines what members can be accessed. 重要的是要理解,引用变量的类型(而不是它引用的对象的类型)决定了可以访问哪些成员。 That is, when a reference to a subclass object is assigned to a superclass reference variable, you will have access only to those parts of the object defined by the superclass. 也就是说,将对子类对象的引用分配给超类引用变量时,您将只能访问由超类定义的对象的那些部分。 This is why plainbox can't access weight even when it refers to a BoxWeight object. 这就是为什么plainbox即使引用BoxWeight对象也无法访问权重的原因。 If you think about it, this makes sense, because the superclass has no knowledge of what a subclass adds to it. 如果您考虑一下,这是有道理的,因为超类不知道子类添加了什么。 This is why the last line of code in the preceding fragment is commented out. 这就是为什么前面片段中的最后一行代码被注释掉的原因。 It is not possible for a Box reference to access the weight field, because Box does not define one. Box引用无法访问权重字段,因为Box没有定义一个。

Now I am Using this example. 现在,我正在使用此示例。 Parent class Box.java 父类Box.java

public class Box{
    int length;
    int breadth;
    int height;
    Box(int length, int breadth, int height){
        this.length = length;
        this.breadth = breadth;
        this.height = height;
    }
    public void getAll(){
        System.out.println("Length"+this.length+"\nBreadth"+this.breadth+"\nHeight"+this.height);
    }
}

Child Class BoxWeight.java 子类BoxWeight.java

public class BoxWeight extends Box{
    int weight;
    BoxWeight(int length, int breadth, int height, int weight){
        super(length,breadth,height);
        this.weight = weight;
    }   
    public void getAll(){
        System.out.println("Length"+this.length+"\nBreadth"+this.breadth+"\nHeight"+this.height+"\nWeight"+this.weight);
    }

    public int getWeight(){
        return this.weight;
    }
}

Implementation Class 实现类

public class Implementation{
    public static void main(String args[]){
        Box simpleBox = new Box(10,10,23);
        BoxWeight boxWeight = new BoxWeight(10,10,10,30);
        System.out.println("box result");
        simpleBox.getAll();
        System.out.println("box weight result");
        boxWeight.getAll();
        simpleBox = new BoxWeight(10,10,10,560);
        System.out.println("Child class reference result");
        simpleBox.getAll();
        //System.out.println(simpleBox.getWeight());
    }
}

The Output is 输出是

box result
Length10
Breadth10
Height23
box weight result
Length10
Breadth10
Height10
Weight30
Child class reference result
Length10
Breadth10
Height10
Weight560

My Question is when I am referencing child class with parent object then why member variable of child class is accessible through parent class object. 我的问题是,当我用父对象引用子类时,为什么可以通过父类对象访问子类的成员变量。 This should not happen according to complete reference java. 根据完整的参考java,这不应发生。

Since getAll() is a public instance method, it is "virtual": the implementation in BoxWeight overrides the implementation in Box . 由于getAll()是公共实例方法,因此它是“虚拟的”: BoxWeight的实现BoxWeight 覆盖 Box的实现。

You have misunderstood the passage that you are reading. 您误解了正在阅读的文章。 The Box class does not "refer" in any way to BoxWeight or BoxWeight.weight or BoxWeight.getAll() ; Box类不会以任何方式“引用” BoxWeightBoxWeight.weightBoxWeight.getAll() ; rather, you're simply calling simpleBox.getAll() , and since simpleBox is an instance of BoxWeight , the relevant implementation of getAll() is the one from BoxWeight . 相反,您只是简单地调用simpleBox.getAll() ,并且由于simpleBoxsimpleBox的实例, BoxWeight getAll()的相关实现是BoxWeight

Where does the member variable of child class is accessed through parent class object in the above example ??? 在上面的示例中,通过父类对象访问子类的成员变量的位置?

After assigning the BoxWeight object to simpleBox (the Box class reference variable), simpleBox only accessed a method getAll() . BoxWeight对象分配给simpleBox (Box类参考变量)后, simpleBox仅访问方法getAll() And the getAll() method called is of the class BoxWeight . 调用的getAll()方法属于BoxWeight类。 It happened because, the getAll() method is there in both super class Box and subcalss BoxWeight , with same signature, which is criteria for method overriding. 发生这种情况的原因是,超类Box和子BoxWeight中都存在具有相同签名的getAll()方法,这是方法覆盖的标准。 So the subclass (BoxWeight ) object , simpleBox overrides the getAll() method. 因此,子类(BoxWeight)对象simpleBox覆盖了getAll()方法。

Thanks all for your support. 感谢大家的支持。 Actually I got the answer, this thing is happening due to dynamic method dispatch. 实际上我得到了答案,这是由于动态方法分派而发生的。 Thanks 谢谢

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

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