从静态方法调用超级方法 [英] calling a super method from a static method

查看:99
本文介绍了从静态方法调用超级方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to call a super static method from child static method?

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

I mean, in a generic way, so far now I have the following:

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(),这似乎不起作用......

and it works, but I'd like to do it in a generic way, something like calling super.loadState(), which doesn't seem to work...

推荐答案

在Java中,不能覆盖静态方法。原因很清楚地解释了 here

In Java, static methods cannot be overidden. The reason is neatly explained here

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

So, it doesn't depend on the object that it is being referenced. But instead, it depends on the type of reference. Hence, static method is said to hide another static method and not override it.

例如(Cat是Animal的子类):

For example (Cat is a subclass of 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.






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


For class methods, the runtime system invokes the method defined in the compile-time type of the reference on which the method is called.

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

In other words, call to static methods are mapped at the compile time and depends on the declared type of the reference (Parent in this case) and not the instance the reference points at runtime. In the example, the compile-time type of myAnimal is Animal. Thus, the runtime system invokes the hide method defined in Animal.

这篇关于从静态方法调用超级方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆