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

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

问题描述

我已经读过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);
        }
    }

此代码打印父。

同时阅读几个地点的数据隐藏概念。

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 their are not hidden by another static variable with the same identifier.

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

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