[英]In Java , is it possible to refer static variable of parent class from child class using super keyword?
Whether it is possible to refer static variable of parent class from child class using super keyword?是否可以使用 super 关键字从子 class 引用父 class 的 static 变量?
Yes, you can use the super.fieldName
.是的,您可以使用
super.fieldName
。 It will compile and run as it should.它将按应有的方式编译和运行。 But it is not necessary to use
super.fieldName
or ParentClassName.fieldName
to access static members of the Superclass from the Subclass.但不必使用
super.fieldName
或ParentClassName.fieldName
从 Subclass 访问 Superclass 的 static 成员。 It can be accessed directly with the fieldName
.可以使用
fieldName
直接访问它。
class Parent {
static int a = 10;
}
class Child extends Parent {
public void print() {
System.out.println(super.a); //valid
System.out.println(Parent.a); //valid
System.out.println(a); //valid
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.