简体   繁体   English

使用super关键字从子类访问超类成员

[英]Accessing superclass members from subclass with super keyword

I have a subclass and a superclass. 我有一个子类和一个超类。 In the subclass when I want to retrieve the values of the super class with super.i and super.one it shows zero . 在子类中,当我想使用super.i和super.one检索超类的值时,它显示为零。 Why? 为什么? Also when I extend a superclass method to a subclass is there absolute necessity to call the superclass member function using super keyword? 另外,当我将超类方法扩展到子类时,绝对有必要使用super关键字调用超类成员函数吗?

public class Inherit{
public static void main(String args[])
{
System.out.println("Hello Inheritance!");
Date now = new Date();
System.out.println(now);
Box hello = new Box(2,3,4);
BoxWeight hello_weight = new BoxWeight(2,5,4,5);
hello.volume();
hello_weight.volume();
Box hello_old = hello;
hello = hello_weight;
//hello.showValues();
hello.show();
hello_old.show();
hello = hello_old;
hello.show();
hello.setValues(7,8);
hello_weight.setValues(70, 80);
hello.showValues();
hello_weight.showValues();
}
}
class Box{
int width, height, depth, i, one;
static int as=0;
Box(int w, int h, int d)
{
    ++as;
    width = w;
    height = h;
    depth = d;
    }
    void setValues(int a, int k)
    {
        i = k;
        one = a;
        System.out.println("The values inside super are : " + i +" " + one +" " +  as);
        }
        void showValues()
        {
            System.out.println("The values of BoxWeight : " + i +" " + one);
            //System.out.println("The superclass values : "+ super.i + " " + super.one);
            }
void volume()
{
System.out.println("Volume : " + width*height*depth);
}
void show()
{
    System.out.println("The height : " + height);
    }
}
class BoxWeight extends Box{
int weight,i,one;
void volume()
{
    System.out.println("Volume and weight : " + width*height*depth +" "+ weight);
    }
    void setValues(int a, int k)
    {
        i = k;
        one = a;
        }
        void showValues()
        {
            System.out.println("The values of BoxWeight : " + i +" " + one);
            System.out.println("The superclass values : "+ super.i + " " + super.one);
            }
BoxWeight(int w, int h, int d, int we)
    {
        super(w,h,d);
        weight = we;
        }
}

because you have not initialized one, so by default it gets value as zero. 因为您尚未初始化1,所以默认情况下它的值为零。

hello_weight 

is object of Box_Weight class, and when you call setValues of that class, one of this class is initailized, and super class one is shadowed . 是Box_Weight类的对象,当您调用该类的setValues时,该类中的一个将被扩展,而上一个类将被阴影化 So super class one is still zero. 所以超类一个仍然是零。

And one is not initialized in constructor. 一个是不是在构造函数初始化。

You don't need the super keyword to access members of the parent class. 您不需要super关键字即可访问父类的成员。 What you do need, however, is to have proper scope / visibility. 但是,您需要做的是具有适当的范围/可见性。

If your parent class has fields that are protected as opposed to private , then only members of the subclass can see them. 如果您的父类具有protected字段,而不是private ,则只有子类的成员才能看到它们。

default scope of a variable is package private. 变量的默认范围是package private. So if you want to access the parent class variables than make is protected or place child and parent both in same packages. 因此,如果您要访问父类变量而不是make protected或者将子代和父代都放在同一包中。

In your case the int width, height, depth, i, one; 在您的情况下, int width, height, depth, i, one; variables are package private so if your Sub class is not inside the same package than it can't access. 变量是程序包私有的,因此,如果您的Sub类不在同一程序包之内,则无法访问。 Hence declare these as protected . 因此,将它们声明为protected

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

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