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

查看:712
本文介绍了什么是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 ++术语

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

这意味着,该对象的动态类型是chosing将调用哪个方法,而不是静态类型。

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天全站免登陆