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
Are static variables inherited
提问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 str
appears in the Child
class, it hides the str
variable of the parent class.
隐藏的概念也适用:当类中str
出现特定于Child
类的str
变量时,它隐藏了父类的变量。
Note that the variable str
of the base class does not become inaccessible: Child
can still access it by fully qualifying with the name of Parent
class.
请注意,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.
在静态的情况下,父属性将被更改,任何其他层次结构也会产生此效果。