静态变量是否继承 [英] Are static variables inherited

查看:39
本文介绍了静态变量是否继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have read at 1000's of locations that Static variables are not inherited. 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);
        }
    }

此代码打印Parent".

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);
    }
}

现在输出是Child".

Now the output is "Child".

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

推荐答案

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

Please have a look into the documentation of 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.

这篇关于静态变量是否继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆