简体   繁体   English

在 Java 中,是否可以使用超级关键字从子 ZA2F2ED4F8EBC2CBB4C21A2 引用父 class 的 static 变量?

[英]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.fieldNameParentClassName.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.

相关问题 我可以使用 Java 中子 class 的 super 关键字为父 class 属性赋值吗? - Can I assign a value to a parent class attribute using super keyword from the child class in Java? 是否可以让子类方法/变量通过父(超级)类运行? (爪哇) - Is it possible to make child class method/variable to run through a parent(Super) Class? (Java) 如何使用子类引用使用父关键字调用父类方法以创建对象? - How to call a parent class method using child class reference using super keyword for object creation? 为什么super不引用super类中的变量? - Why does super not refer to the variable in super class? 在子类中使用超级类中的枚举 - Using Enums from Super Class in Child Class Java:使用父类方法访问子类变量 - Java : Using parent class method to access child class variable 使用super()或对象访问父类中的变量? - Access variable in parent class using super(), or an object? 子类在Java中从Parent类更改受保护的变量 - Child class Changing a protected variable from a Parent class in Java 当我们创建子类对象时,super如何引用父类对象? - how can super refer to parent class object when we create a child class object? 为什么我不能在静态上下文中使用“super”变量,即使“super”指的是父类而不是类实例,与“this”不同? - Why can I not use “super” variable from a static context, even though “super” refers to the parent class and NOT a class instance, unlike “this”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM