![](/img/trans.png)
[英]Can I assign a value to a parent class attribute using super keyword from the child class in Java?
[英]In Java , is it possible to refer static variable of parent class from child class using super keyword?
是否可以使用 super 关键字从子 class 引用父 class 的 static 变量?
是的,您可以使用super.fieldName
。 它将按应有的方式编译和运行。 但不必使用super.fieldName
或ParentClassName.fieldName
从 Subclass 访问 Superclass 的 static 成员。 可以使用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.