我可以覆盖和重载 Java 中的静态方法吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2475259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I override and overload static methods in Java?
提问by giri
I'd like to know:
我想知道:
- Why can't static methods be overridden in Java?
- Can static methods be overloaded in Java?
- 为什么不能在 Java 中覆盖静态方法?
- 静态方法可以在 Java 中重载吗?
回答by Thilo
Static methods cannot be overridden because they are not dispatched on the object instance at runtime. The compiler decides which method gets called.
静态方法不能被覆盖,因为它们不是在运行时在对象实例上调度的。编译器决定调用哪个方法。
This is why you get a compiler warning when you write
这就是为什么您在编写时会收到编译器警告
MyClass myObject = new MyClass();
myObject.myStaticMethod();
// should be written as
MyClass.myStaticMethod()
// because it is not dispatched on myObject
myObject = new MySubClass();
myObject.myStaticMethod();
// still calls the static method in MyClass, NOT in MySubClass
Static methods can be overloaded (meaning that you can have the same method name for several methods as long as they have different parameter types).
静态方法可以被重载(这意味着您可以为多个方法使用相同的方法名称,只要它们具有不同的参数类型)。
Integer.parseInt("10");
Integer.parseInt("AA", 16);
回答by VolatileDream
Static methods can not be overridden because there is nothing to override, as they would be two different methods. For example
静态方法不能被覆盖,因为没有什么可以覆盖,因为它们是两种不同的方法。例如
static class Class1 {
public static int Method1(){
return 0;
}
}
static class Class2 extends Class1 {
public static int Method1(){
return 1;
}
}
public static class Main {
public static void main(String[] args){
//Must explicitly chose Method1 from Class1 or Class2
Class1.Method1();
Class2.Method1();
}
}
And yes static methods can be overloaded just like any other method.
是的,静态方法可以像任何其他方法一样重载。
回答by opensas
Static methods can not be overridden in the exact sense of the word, but they can hide parent static methods
严格意义上的静态方法不能被覆盖,但它们可以隐藏父静态方法
In practice it means that the compiler will decide which method to execute at the compile time, and not at the runtime, as it does with overridden instance methods.
在实践中,这意味着编译器将决定在编译时执行哪个方法,而不是在运行时,就像重写实例方法那样。
For a neat example have a look here.
一个整洁的例子看看这里。
And thisis java documentation explaining the difference between overridinginstance methods and hidingclass (static) methods.
而这是Java文档解释之间的区别重写实例方法和隐藏的类(静态)方法。
Overriding:Overriding in Java simply means that the particular method would be called based on the run time type of the object and not on the compile time type of it (which is the case with overriden static methods)
Hiding:Parent class methods that are static are not part of a child class (although they are accessible), so there is no question of overriding it. Even if you add another static method in a subclass, identical to the one in its parent class, this subclass static method is unique and distinct from the static method in its parent class.
覆盖:在 Java 中覆盖只是意味着将根据对象的运行时类型而不是它的编译时类型调用特定方法(这是覆盖静态方法的情况)
隐藏:静态的父类方法不是子类的一部分(尽管它们是可访问的),因此没有覆盖它的问题。即使您在子类中添加另一个与其父类中的静态方法相同的静态方法,该子类的静态方法也是唯一的,并且不同于其父类中的静态方法。
回答by Manoranjan
Static methods can not be overridden because they are not part of the object's state. Rather, they belongs to the class (i.e they are class methods). It is ok to overload static (and final) methods.
静态方法不能被覆盖,因为它们不是对象状态的一部分。相反,它们属于类(即它们是类方法)。可以重载静态(和最终)方法。
回答by Dalee Bisen
If I m calling the method by using SubClass name MysubClass then subclass method display what it means static method can be overridden or not
如果我使用子类名称 MysubClass 调用该方法,则子类方法显示静态方法可以被覆盖或不被覆盖的含义
class MyClass {
static void myStaticMethod() {
System.out.println("Im in sta1");
}
}
class MySubClass extends MyClass {
static void myStaticMethod() {
System.out.println("Im in sta123");
}
}
public class My {
public static void main(String arg[]) {
MyClass myObject = new MyClass();
myObject.myStaticMethod();
// should be written as
MyClass.myStaticMethod();
// calling from subclass name
MySubClass.myStaticMethod();
myObject = new MySubClass();
myObject.myStaticMethod();
// still calls the static method in MyClass, NOT in MySubClass
}
}
回答by Kumar Manish
Parent class methods that are static are not part of a child class (although they are accessible), so there is no question of overriding it. Even if you add another static method in a subclass, identical to the one in its parent class, this subclass static method is unique and distinct from the static method in its parent class.
静态的父类方法不是子类的一部分(尽管它们是可访问的),因此没有覆盖它的问题。即使您在子类中添加另一个与其父类中的静态方法相同的静态方法,该子类的静态方法也是唯一的,并且不同于其父类中的静态方法。
回答by Sathesh
The very purpose of using the static method is to access the method of a class without creating an instance for it.It will make no sense if we override that method since they will be accessed by classname.method()
使用静态方法的真正目的是访问类的方法而不为其创建实例。如果我们覆盖该方法将毫无意义,因为它们将通过 classname.method() 访问
回答by praveen
No,Static methods can't be overriden as it is part of a class rather than an object. But one can overload static method.
不,静态方法不能被覆盖,因为它是类的一部分而不是对象。但是可以重载静态方法。
回答by user2500552
No, you cannot override a static method. The static resolves against the class, not the instance.
不,您不能覆盖静态方法。静态解析为类,而不是实例。
public class Parent {
public static String getCName() {
return "I am the parent";
}
}
public class Child extends Parent {
public static String getCName() {
return "I am the child";
}
}
Each class has a static method getCName(). When you call on the Class name it behaves as you would expect and each returns the expected value.
每个类都有一个静态方法 getCName()。当您调用类名称时,它的行为与您预期的一样,并且每个都返回预期值。
@Test
public void testGetCNameOnClass() {
assertThat(Parent.getCName(), is("I am the parent"));
assertThat(Child.getCName(), is("I am the child"));
}
No surprises in this unit test. But this is not overriding.This declaring something that has a name collision.
在这个单元测试中没有惊喜。但这不是覆盖。这声明了名称冲突的东西。
If we try to reach the static from an instance of the class (not a good practice), then it really shows:
如果我们尝试从类的实例访问静态(不是一个好习惯),那么它确实表明:
private Parent cp = new Child();
`enter code here`
assertThat(cp.getCName(), is("I am the parent"));
Even though cp is a Child, the static is resolved through the declared type, Parent, instead of the actual type of the object. For non-statics, this is resolved correctly because a non-static method can override a method of its parent.
即使 cp 是 Child ,静态也是通过声明的类型 Parent 解析的,而不是对象的实际类型。对于非静态,这是正确解决的,因为非静态方法可以覆盖其父级的方法。
回答by charles_ma
You can overload a static method but you can't override a static method. Actually you can rewrite a static method in subclasses but this is not called a override because override should be related to polymorphism and dynamic binding. The static method belongs to the class so has nothing to do with those concepts. The rewrite of static method is more like a shadowing.
您可以重载静态方法,但不能覆盖静态方法。实际上,您可以在子类中重写静态方法,但这不称为覆盖,因为覆盖应该与多态性和动态绑定相关。静态方法属于类,因此与这些概念无关。静态方法的重写更像是一个阴影。