java中的虚方法调用是什么? [英] What is virtual method calling in java?

查看:22
本文介绍了java中的虚方法调用是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一些计算机科学测试中看到了下一段,我希望我能在这里得到一个很好的解释,因为我用谷歌搜索了一个小时,但找不到任何东西..

Ive saw the next paragraph in some computer science test and i'll hope i can get here a good explanation of what it means because i googled it for an hour and can't find anything..

当我们说 Java 语言具有虚拟方法调用时,我们的意思是在 Java 应用程序中执行的方法由运行时的对象类型决定"

"When we say Java language has virtual method calling we mean that in java applications the executed method is determined by the object type in run time"

什么意思?谁能解释得更好?

What does it mean? can anyone explain it better?

推荐答案

这些行的作者使用了 virtual 的 C++ 术语.

The author of these lines used the c++ terminology of virtual.

更好的术语是动态绑定/动态调度.

这意味着,对象的动态 type 正在选择"将调用哪个方法,而不是静态类型.

That means, that the object's dynamic type is "chosing" which method will be invoked, and not the static type.

例如:[伪代码]:

class A {
  public void foo() { }
}
class B extends A { 
  public void foo() { }
}

调用时:

A obj = new B();
obj.foo();

B.foo() 将被调用,而不是 A.foo(),因为 obj 的动态类型是 B.

B.foo() will be invoked, and NOT A.foo(), since the dynamic type of obj is B.

这篇关于java中的虚方法调用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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