静态方法重写
class XYZ{
public static void show(){
System.out.println("inside XYZ");
}
}
public class StaticTest extends XYZ {
public static void show() {
System.out.println("inside statictest");
}
public static void main(String args[]){
StaticTest st =new StaticTest();
StaticTest.show();
}
}
尽管我们知道静态方法不能被重写。那么到底发生了什么?
class XYZ{
public static void show(){
System.out.println("inside XYZ");
}
}
public class StaticTest extends XYZ {
public static void show() {
System.out.println("inside statictest");
}
public static void main(String args[]){
StaticTest st =new StaticTest();
StaticTest.show();
}
}
though we know static methods cant be overridden. Then what actually is happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
静态方法属于类。它们不能被覆盖。但是,如果在子类中定义了与父类静态方法具有相同签名的方法,则会隐藏父类方法。
StaticTest.show()
隐藏了XYZ.show()
方法,因此StaticTest.show()
是在代码中的main
方法。Static methods belong to the class. They can't be overridden. However, if a method of the same signature as a parent class static method is defined in a child class, it hides the parent class method.
StaticTest.show()
is hiding theXYZ.show()
method and soStaticTest.show()
is the method that gets executed in themain
method in the code.它不是
覆盖
,它们是两个不同类中具有相同签名的两个不同方法。但是来自 XYZ 的方法不能通过继承在子类中使用。它将调用
StaticTest
中的方法Its not
overriding
they are two different method in two different class with same signature. but method fromXYZ
isn't available in child class through inheritance .It will call method from
StaticTest
说它没有被正确覆盖...静态方法与类“绑定”,因此
它们
是两个完全不同的东西。请注意,您不能调用 super.show()
It's not overriden properly said... Static methods are 'tied' to the class so
and
are two totally different things. Note you can't invoke super.show()
要看到差异,您必须使用更强大的示例:
Samit G. 已经编写了具有相同签名的静态方法在基类和派生类中都隐藏了实现,这不是最重要的。您可以通过将一个或另一个静态方法更改为非静态方法或将它们都更改为非静态来玩一下该示例,以查看 java 编译器会出现哪些编译错误。
To see the difference you have to use more powerful example:
As Samit G. already have written static methods with same signature in both base and derived classes hide the implementation and this is no-overriding. You can play a bit with the example by changing the one or the another of the static methods to non-static or changing them both to non-static to see what are the compile-errors which the java compiler rises.
它不是重写,而是隐藏 XYZ 中的方法的单独方法。
It's not an override, but a separate method that hides the method in
XYZ
.据我所知,任何静态成员(方法或状态)都是类的属性,并且不会与类的任何实例关联。因此,在您的示例中,XYZ 是一个类,StaticTest 也是一个类(如您所知)。因此,通过调用构造函数,首先会发生两件事。创建了一个 Class 类型的对象。它有一个成员调用 shown()。类 XYZ.class 是从 Object 扩展而来的,因此它具有所有这些 Object 方法以及 show()。与 StaticClass 相同,类对象上也有 show() 。不过,它们都扩展了 java.lang.Object。 StaticClass 的实例也是 XYZ 的实例。然而现在更有趣的问题是当你在 st 上调用 show() 时会发生什么?
那里会发生什么?我的猜测是第一次是 StaticClass.show() ,第二次是 XYZ.show() 。
So as I know, any static member (method or state) is an attribute of a class, and would not be associated with any instance of a class. So in your example, XYZ is a class, and so is StaticTest (as you know). So by calling the constructor two things first happen. An Object of type Class is created. It has a member on it call showed(). Class, XYZ.class, extends from Object so has all those Object methods on it plus show(). Same with the StaticClass, the class object has show() on it as well. They both extend java.lang.Object though. An instance of StaticClass would also be an instance of XYZ. However now the more interesting question would be what happens when you call show() on st?
What happens there? My guess is that it is StaticClass.show() the first time and XYZ.show() the second.
静态方法与类相关,而不是与实例(对象)相关。
因此,调用始终是 ClassName.staticMethod();
当子类中出现相同静态方法的这种情况时,称为精炼(重新定义)静态方法而不是重写。
Static methods are tied to classes and not instances (objects).
Hence the invocations are always ClassName.staticMethod();
When such a case of same static method in a subclass appears, its called as refining (redefining) the static method and not overriding.
// Java 允许从实例/对象引用调用静态方法
// 在其他纯 OOP 语言(如 C# Dot net)中情况并非如此。
// 这导致了这种混乱。
// 从技术上讲,静态方法始终与类相关联,而不是与实例相关联。
// 换句话说,静态函数的绑定是在编译时进行的。 - 早期绑定
//
// 例如。
//
//
// 因此,即使在这种情况下,使用实际引用的实例 b1 进行调用
// SuperClass 类型的对象,它调用 BaseClass:f1 方法。
//
// Java allows a static method to be called from an Instance/Object reference
// which is not the case in other pure OOP languages like C# Dot net.
// which causes this confusion.
// Technically, A static method is always tied to a Class and not instance.
// In other words, the binding is at compile-time for static functions. - Early Binding
//
// eg.
//
//
// So even though in this case, called with an instance b1 which is actually referring to
// an object of type SuperClass, it calls the BaseClass:f1 method.
//