java 静态变量是否继承

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37226269/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:19:10  来源:igfitidea点击:

Are static variables inherited

javainheritancestaticdata-hiding

提问by Aman

I have read at 1000's of locations that Static variables are not inherited. But then how this code works fine?

我已经阅读了 1000 多个未继承静态变量的位置。但是这段代码如何正常工作呢?

Parent.java

父程序

public class Parent {
        static String str = "Parent";
    }

Child.java

子程序

public class Child extends Parent {
        public static void main(String [] args)
        {
            System.out.println(Child.str);
        }
    }

This code prints "Parent".

此代码打印“Parent”。

Also read at few locations concept of data hiding.

还可以在几个位置阅读数据隐藏的概念。

Parent.java

父程序

public class Parent {
    static String str = "Parent";
}

Child.java

子程序

public class Child extends Parent {
    static String str = "Child";

    public static void main(String [] args)
    {
        System.out.println(Child.str);
    }
}

Now the output is "Child".

现在输出是“孩子”。

So does this mean that static variables are inherited but they follow the concept of data-hiding?

那么这是否意味着静态变量是继承的,但它们遵循数据隐藏的概念

采纳答案by FlorianSchunke

Please have a look into the documentation of oracle: http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#d5e12110

请查看oracle的文档:http: //docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#d5e12110

Static variables are inherited as long as they're are not hidden by another static variable with the same identifier.

只要静态变量不被具有相同标识符的另一个静态变量隐藏,它们就会被继承。

回答by dasblinkenlight

"Inherited"is not an ideal description of what is happening; a better way to describe it would be to say that static variables are sharedamong the subclasses of the base class.

“继承”并不是对正在发生的事情的理想描述;描述它的更好方法是说静态变量在基类的子类之间共享

All derived classes obtain access to static variables of their base classes. This includes protected variables, mirroring the situation with variables that are inherited.

所有派生类都可以访问其基类的静态变量。这包括受保护的变量,用继承的变量反映情况。

The concept of hiding applies as well: when a class-specific variable strappears in the Childclass, it hides the strvariable of the parent class.

隐藏的概念也适用:当类中str出现特定于Child类的str变量时,它隐藏了父类的变量。

Note that the variable strof the base class does not become inaccessible: Childcan still access it by fully qualifying with the name of Parentclass.

请注意,str基类的变量不会变得不可访问:Child仍然可以通过使用Parent类名进行完全限定来访问它。

回答by Amer Qarabsa

This is not exactly inheritance, its more like sharing having access to the static attribute of the class you are extending unless you are hiding it by declaring the same identifier in you subclass, note that in case of instance attribute if you change the value of the inherited attribute it will be changed in the super instance which was instantiated for your object but if there is another hierarchy which will be supposedly blind to your hierarchy it will not be affected.

这不完全是继承,它更像是共享访问您正在扩展的类的静态属性,除非您通过在子类中声明相同的标识符来隐藏它,请注意,如果您更改了实例属性的值继承的属性它将在为您的对象实例化的超级实例中更改,但如果有另一个层次结构可能对您的层次结构视而不见,则不会受到影响。

In the case of static the parent attribute will be changed and any other hierarchy will take this effect too.

在静态的情况下,父属性将被更改,任何其他层次结构也会产生此效果。