从静态方法调用超级方法

是否可以从子静态方法调用超静态方法?

我的意思是,以通用的方式,到目前为止,我有以下内容:

public class BaseController extends Controller { static void init() { //init stuff } } public class ChildController extends BaseController { static void init() { BaseController.loadState(); // more init stuff } } 

它的工作原理,但我想以通用的方式做,比如调用super.loadState(),这似乎不起作用……

在Java中,不能覆盖静态方法。 原因在这里得到了很好的解释

因此,它不依赖于它被引用的对象。 但相反,它取决于参考的类型。 因此,静态方法被称为隐藏另一个静态方法而不是覆盖它。

例如(Cat是Animal的子类):

 public class Animal { public static void hide() { System.out.format("The hide method in Animal.%n"); } public void override() { System.out.format("The override method in Animal.%n"); } } public class Cat extends Animal { public static void hide() { System.out.format("The hide method in Cat.%n"); } public void override() { System.out.format("The override method in Cat.%n"); } } 

主要课程:

 public static void main(String[] args) { Cat myCat = new Cat(); System.out.println("Create a Cat instance ..."); myCat.hide(); Cat.hide(); myCat.override(); Animal myAnimal = myCat; System.out.println("\nCast the Cat instance to Animal..."); Animal.hide(); myAnimal.override(); Animal myAnimal1 = new Animal(); System.out.println("\nCreate an Animal instance...."); Animal.hide(); myAnimal.override(); } 

现在,输出将如下所示

 Create a Cat instance ... The hide method in Cat. The hide method in Cat. The override method in Cat. Cast the Cat instance to Animal... The hide method in Animal. The override method in Cat. Create an Animal instance.... The hide method in Animal. The override method in Animal. 

对于class methods ,运行时系统调用在调用方法的引用的编译时类型中定义的方法。

换句话说,对静态方法的调用是在编译时映射的,并且取决于引用的声明类型(在本例中为Parent),而不是运行时引用指向的实例。 在该示例中, myAnimal的编译时类型是Animal 。 因此,运行时系统调用Animal定义的hide方法。

Java中有静态inheritance 。 改编尼基塔的例子:

 class A { static void test() { System.out.print("A"); } } class B extends A { } class C extends B { static void test() { System.out.print("C"); B.test(); } public static void main(String[] ignored) { C.test(); } } 

这当然编译,并调用C打印“CA”,当然。 现在我们将B类更改为:

 class B extends A { static void test() { System.out.print("B"); } } 

并重新编译B(不是C)。 现在再次调用C,它将打印“CB”。

静态方法没有super类似的关键字 – 但是(坏的)理由可能是“超类的名称是在这个类的声明中写的,所以你不得不重新编译你的类来改变它,所以你也可以在这里更改静态调用。“

整个inheritance概念不适用于Java中的静态元素。 例如,静态方法不能覆盖另一个静态方法。
所以,不,你必须通过名称调用它或使它们成为某个对象的实例方法。 (您可能希望特别查看其中一种工厂模式)。

一个实际的例子

 class A { static void test() { System.out.println("A"); } } class B extends A { static void test() { System.out.println("B"); } } A a = new B(); B b = new B(); a.test(); b.test(); 

这打印A然后打印B 即,被调用的方法取决于如何声明变量而不是其他。

实际上,您可以通过方法名称及其参数来调用超类的静态方法。

 public class StaticTest { public static void main(String[] args) { NewClass.helloWorld(); } } public class NewClass extends BaseClass { public static void helloWorld() { try { NewClass.class.getSuperclass().getMethod("helloWorld", new Class[] {}).invoke( NewClass.class ,new Object[]{} ); } catch (Exception e) { e.printStackTrace(); } System.out.println("myVar = " + myVar); } } public class BaseClass extends BaseBaseClass { protected static String myVar; public static void helloWorld() { System.out.println("Hello from Base"); myVar = "Good"; } } 

这应该工作,并且在子类中,您可以在基类中设置所有内容。

输出应该是:

来自Base的你好

myVar =好

您的实现的官方名称称为方法隐藏 。 我建议引入一个静态init(Controller controller)方法,并调用一个实例方法来利用覆盖。

 public class Controller { static void init(Controller controller) { controller.init(); } void init() { //init stuff } } public class BaseController extends Controller { @override void init() { super.init(); //base controller init stuff } } public class ChildController extends BaseController { @override void init() { super.init(); //child controller init stuff } } 

然后,您可以调用Controller.init(controllerInstance)。

对于静态方法,不需要类的实例,因此没有超级。