About inheritance in Java static properties and static methods

First look at a piece of code to taste

// 父类
public class Fu {
    public static String static_name;
    public static void staticMethod(){
        System.out.println("子类静态方法");
    }
}
// 子类
public class Zi extends Fu{

}
public class Main {
    public static void main(String[] args) {
        Zi.staticMethod();  // 子类可以访问父类静态方法
        Fu.static_name = "hello";  //给父类静态属性赋值之后,子类调用它时,直接输出父类给他赋的值
        System.out.println(Zi.static_name);
        Zi.static_name = "world";  // 子类给静态属性重新赋值
        System.out.println(Fu.static_name);  // 父类静态属性的值也改变了
    }
}

Output is as follows:

子类静态方法
hello
world

This article does not say whether the inheritance relations, and he is not sure, but by these few lines can help you understand how the relationship between them.

Specifically whether inheritance, and so we determined later, to write.

Here are good articles explain this stuff, and you can refer to:
the venue entrance

Published 141 original articles · won praise 131 · views 210 000 +

Guess you like

Origin blog.csdn.net/qq_41621362/article/details/103115722