静态方法重写

发布于 2024-11-07 18:26:21 字数 402 浏览 5 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

爱,才寂寞 2024-11-14 18:26:21

静态方法属于类。它们不能被覆盖。但是,如果在子类中定义了与父类静态方法具有相同签名的方法,则会隐藏父类方法。 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 the XYZ.show() method and so StaticTest.show() is the method that gets executed in the main method in the code.

农村范ル 2024-11-14 18:26:21

它不是覆盖,它们是两个不同类中具有相同签名的两个不同方法。但是来自 XYZ 的方法不能通过继承在子类中使用。

它将调用 StaticTest 中的方法

Its not overriding they are two different method in two different class with same signature. but method from XYZ isn't available in child class through inheritance .

It will call method from StaticTest

谎言 2024-11-14 18:26:21

说它没有被正确覆盖...静态方法与类“绑定”,因此

StaticTest.show();

它们

XYZ.show();

是两个完全不同的东西。请注意,您不能调用 super.show()

It's not overriden properly said... Static methods are 'tied' to the class so

StaticTest.show();

and

XYZ.show();

are two totally different things. Note you can't invoke super.show()

聽兲甴掵 2024-11-14 18:26:21

要看到差异,您必须使用更强大的示例:

class Super {
    public static void hidden(Super superObject) {
        System.out.println("Super-hidden");
        superObject.overriden();
    }

    public void overriden() {
        System.out.println("Super-overriden");
    }
}

class Sub extends Super {
    public static void hidden(Super superObject) {
        System.out.println("Sub-hidden");
        superObject.overriden();
    }

    public void overriden() {
        System.out.println("Sub-overriden");
    }
}

public class Test {
    public static void main(String[] args) {
        Super superObject = new Sub();
        superObject.hidden(superObject);
    }
}

Samit G. 已经编写了具有相同签名的静态方法在基类和派生类中都隐藏了实现,这不是最重要的。您可以通过将一个或另一个静态方法更改为非静态方法或将它们都更改为非静态来玩一下该示例,以查看 java 编译器会出现哪些编译错误。

To see the difference you have to use more powerful example:

class Super {
    public static void hidden(Super superObject) {
        System.out.println("Super-hidden");
        superObject.overriden();
    }

    public void overriden() {
        System.out.println("Super-overriden");
    }
}

class Sub extends Super {
    public static void hidden(Super superObject) {
        System.out.println("Sub-hidden");
        superObject.overriden();
    }

    public void overriden() {
        System.out.println("Sub-overriden");
    }
}

public class Test {
    public static void main(String[] args) {
        Super superObject = new Sub();
        superObject.hidden(superObject);
    }
}

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.

意中人 2024-11-14 18:26:21

据我所知,任何静态成员(方法或状态)都是类的属性,并且不会与类的任何实例关联。因此,在您的示例中,XYZ 是一个类,StaticTest 也是一个类(如您所知)。因此,通过调用构造函数,首先会发生两件事。创建了一个 Class 类型的对象。它有一个成员调用 shown()。类 XYZ.class 是从 Object 扩展而来的,因此它具有所有这些 Object 方法以及 show()。与 StaticClass 相同,类对象上也有 show() 。不过,它们都扩展了 java.lang.Object。 StaticClass 的实例也是 XYZ 的实例。然而现在更有趣的问题是当你在 st 上调用 show() 时会发生什么?

StaticClass st = new StaticClass();
st.show();
XYZ xyz = st;
xyz.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?

StaticClass st = new StaticClass();
st.show();
XYZ xyz = st;
xyz.show();

What happens there? My guess is that it is StaticClass.show() the first time and XYZ.show() the second.

风追烟花雨 2024-11-14 18:26:21

静态方法与类相关,而不是与实例(对象)相关。

因此,调用始终是 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.

蝶舞 2024-11-14 18:26:21

// Java 允许从实例/对象引用调用静态方法
// 在其他纯 OOP 语言(如 C# Dot net)中情况并非如此。
// 这导致了这种混乱。
// 从技术上讲,静态方法始终与类相关联,而不是与实例相关联。
// 换句话说,静态函数的绑定是在编译时进行的。 - 早期绑定
//
// 例如。

class BaseClass 
{
 public static void f1()
 {
  System.out.println("BaseClass::f1()...");
 } // End of f1().
}

public class SubClass extends BaseClass 
{
  public static void f1() 
  {
    System.out.println("SubClass::f1()...");
    // super.f1(); // non-static variable super cannot be referenced from a static context

  } // End of f1().

  public static void main(String[] args)
  {
f1();
SubClass obj1 = new SubClass();
obj1.f1();
BaseClass b1 = obj1;
b1.f1();

  } // End of main().

} // End of class.

// Output:
// SubClass::f1()...
// SubClass::f1()...
// BaseClass::f1()...

//
//
// 因此,即使在这种情况下,使用实际引用的实例 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.

class BaseClass 
{
 public static void f1()
 {
  System.out.println("BaseClass::f1()...");
 } // End of f1().
}

public class SubClass extends BaseClass 
{
  public static void f1() 
  {
    System.out.println("SubClass::f1()...");
    // super.f1(); // non-static variable super cannot be referenced from a static context

  } // End of f1().

  public static void main(String[] args)
  {
f1();
SubClass obj1 = new SubClass();
obj1.f1();
BaseClass b1 = obj1;
b1.f1();

  } // End of main().

} // End of class.

// Output:
// SubClass::f1()...
// SubClass::f1()...
// BaseClass::f1()...

//
//
// 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.
//

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文