简体   繁体   English

继承的子类中的Java公共/受保护的变量值为Null

[英]Java public / protected variable values inside inherited subclass is Null

I want to access a variable declared protected in Superclass, from subclass. 我想从子类访问在超类中声明为受保护的变量。

The values are displayed null in subclass, though it displays correctly in superclass. 值在子类中显示为null ,尽管在父类中正确显示。 If the superclass variable is static, it works, but the loop values are not taken correctly. 如果超类变量是静态的,则它可以工作,但是循环值不能正确使用。

My code: 我的代码:

public class App {
    public String var ;

    public static void main(String[] args) {    
        App ap = new App();
        ap.fromt();
    }

    public void fromt() {
        Sub s = new Sub();
        for(int i = 0; i <= 5; i++) {
            var = "Test" + i;
            System.out.println("Hello World!" + var);
            s.testtt();
        }
    }
}

//**Inherited Class**//
public class Sub extends App {
    public void testtt() {
        String var2 = super.var;
        System.out.println("Hello World! Subclass" + var2);
    }
}

Output : 输出:

Hello World!Test0
Hello World! Subclassnull
Hello World!Test1
Hello World! Subclassnull
Hello World!Test2
Hello World! Subclassnull
Hello World!Test3
Hello World! Subclassnull
Hello World!Test4
Hello World! Subclassnull
Hello World!Test5
Hello World! Subclassnull

The classes Sub and App do get their own the copies of the member variable var defined in the superclass. SubApp类确实获得了自己在超类中定义的成员变量var的副本。 They are 2 different copies. 它们是2个不同的副本。 Any changes made to one of the copies will remain mutually exclusive to the other. 对其中一份副本所做的任何更改将始终与另一份副本互斥。 The things change when static members are used. 使用静态成员时情况会发生变化。 Sharing of member data in inheritance should not be confused while the classes are instantiated. 在实例化类时,不要混淆继承中的成员数据共享。

To answer your doubt, String var is not initialised anywhere so assumes a null value by default. 为了回答您的疑问, String var不会在任何地方初始化,因此默认情况下采用null值。 If it is initialised while declaring or in a constructor of the super class App , the value will be reflected in the instance of Sub as well. 如果在声明时或在超类App的构造函数中对其进行了初始化,则该值也将反映在Sub的实例中。

You're dealing with a matter of scope here. 您在这里处理范围问题。 You have the instance of App that is running, and within that instance you are running your loop and setting the value of your variable within this specific instance. 您有正在运行的App实例,并且在该实例内您正在运行循环并在此特定实例内设置变量的值。 Then you are creating an instance of Sub that is currently maintaining its own instance of var within itself. 然后,您将创建Sub的实例,该实例当前在其内部维护自己的var实例。

Now, if you want a singular instance of var shared across all instances of App/Sub then you would make it: 现在,如果要在App / Sub的所有实例之间共享var的单个实例,则可以这样做:

public static String var ;

I am assuming that this is for a homework assignment so I won't go into the pros and cons of this, but just know that static vars are one of those things you really have to think about for any sort of app. 我假设这是用于家庭作业的,所以我不会去讨论它的利弊,但只知道静态var是您对于任何类型的应用程序都必须考虑的事情之一。

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

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