java - 继承静态方法的概念

标签 java

我有以下 2 个类(class)

public class statictest {

public void print()
{
    System.out.println("first one");    
}
  }

public class newer extends statictest
 {

public void print()
{

    System.out.println("second one");

}   
  }

在主函数中我所做的

statictest temp = new newer();
newer temp2 = new newer();

temp.print();
temp2.print();  

输出是:

second one
second one

但是当我将这两个方法设为静态时,输出为

firstone
secondone

在这种情况下后期绑定(bind)发生了什么?谁能解释一下

最佳答案

这称为动态方法调用。您可以查看this JLS。 它指出,

The strategy for method lookup depends on the invocation mode.

If the invocation mode is static, no target reference is needed and overriding is not allowed. Method m of class T is the one to be invoked.

Otherwise, an instance method is to be invoked and there is a target reference. If the target reference is null, a NullPointerException is thrown at this point. Otherwise, the target reference is said to refer to a target object and will be used as the value of the keyword this in the invoked method. The other four possibilities for the invocation mode are then considered.

关于java - 继承静态方法的概念,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15779160/

相关文章:

java - 如何在按键后解析下一个节点属性(Java DOM Parser)

java - 小程序调用中的参数大小太大

java - 哈希码返回整数值

java - 图形上的图像

java - 我们如何通过java代码访问计算机的控制面板

java - 是否可以有 2 个文件替代 log4j,每次打开时使用?

java - JPanel 未添加到 MenuSelected 中

java - Android 教程应用程序无法运行

java - 将 Java 应用程序连接到 Ruby on Rails SQLite3 数据库

java - application.properties 与 applicationContext.xml