在Java中重新定义静态方法意味着什么? [英] What does redefining static methods mean in Java?

查看:107
本文介绍了在Java中重新定义静态方法意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在SCJP学习指南中阅读有关Statics的部分,它提到了以下内容:

I've been reading a section on Statics in the SCJP study guide, and it mentions the following :


静态方法可以'被覆盖,
但是可以重新定义

static methods can't be overridden, but they can be redefined

重新定义实际意味着什么?是否存在父和子都存在的静态方法,具有相同的签名,但是它们的类名分别引用它们?例如:

What does redefining actually mean? Is it a case of having a static method that exists in both parent and child, with the same signature, however they are referenced separately by their class names? Such as :

class Parent
{
   static void doSomething(String s){};
}

class Child extends Parent
{
   static void doSomething(String s){};
}

参考: Parent.doSomething(); Child.doSomething();

此外,这同样适用于静态变量,或只是静态方法?

Also, does the same apply for static variables, or just static methods?

推荐答案

它只是意味着这些函数不是虚拟的。例如,假设您有一个(运行时)类型的对象Child,它是从Parent类型的变量引用的。然后,如果您调用 doSomething ,则调用Parent的 doSomething 方法:

It simply means that the functions are not virtual. As an example, say that you have an object of (runtime) type Child which is referenced from a variable of type Parent. Then if you invoke doSomething, the doSomething method of the Parent is invoked:

Parent p = new Child();
p.doSomething(); //Invokes Parent.doSomething

如果方法是非静态的, doSomething of Child会覆盖Parent的, child.doSomething 会被调用。

If the methods were non-static, doSomething of Child would override that of Parent and child.doSomething would have been invoked.

静态字段也是如此。

这篇关于在Java中重新定义静态方法意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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