简体   繁体   English

静态变量是否继承

[英]Are static variables inherited

I have read at 1000's of locations that Static variables are not inherited.我已经阅读了 1000 多个未继承静态变量的位置。 But then how this code works fine?但是这段代码如何正常工作呢?

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".此代码打印“父”。

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 ?那么这是否意味着静态变量是继承的,但它们遵循数据隐藏的概念

"Inherited" is not an ideal description of what is happening; “继承”并不是对正在发生的事情的理想描述; a better way to describe it would be to say that static variables are shared among 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.隐藏的概念也适用:当Child类中出现特定于类的变量str ,它隐藏了父类的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类的名称进行完全限定来访问它。

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.只要静态变量不被具有相同标识符的另一个静态变量隐藏,它们就会被继承。

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.在静态的情况下,父属性将被更改,任何其他层次结构也会产生此效果。

inheritance is between parent class object and child class object (not class but objects) .继承是在父类对象和子类对象(不是类而是对象)之间。 class is not an actual thing but template(structire only) and when you create a child class the teplate is just enlarged also having old structute(here, static method)类不是实际的东西,而是模板(仅限结构),当你创建一个子类时,teplate 只是放大了,也有旧的结构(这里是静态方法)

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

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