问题描述
为什么Java的功能是通过虚拟default.But C#其oposite。
Why java functions are virtual by default.But c# its the oposite.
哪个更好?或什么是在这两个优势和劣势?
Which is better?Or what is the advantage and disadvantage in both?
感谢
SC
推荐答案
(C#首席架构师)
有几个原因。一个是
的性能。我们可以观察到,如
人在Java编写代码,他们忘记了
,以纪念他们的方法决赛。
因此,这些方法都是虚拟的。
,因为他们是虚拟的,他们没有
进行为好。这里还有与
是一个虚拟的方法相关
的性能开销。这的One
的问题。
一个更重要的问题就是版本。
有大约
虚方法两个思想流派。思想的学术流派
说:一切都应该是
虚拟的,因为我可能要
改写这一天。思想的务实
学校,B栋在
现实世界中运行的实际应用它来自$ B $,说:我们还必须要
实际小心什么,我们做
虚拟。
A more important issue is versioning. There are two schools of thought about virtual methods. The academic school of thought says, "Everything should be virtual, because I might want to override it someday." The pragmatic school of thought, which comes from building real applications that run in the real world, says, "We've got to be real careful about what we make virtual."
当我们做了
平台虚拟的东西,我们正在做一个可怕的很多
的承诺如何它的发展在
的未来。对于非虚方法,我们
的承诺,当你调用这个
法,x和y会发生。当我们
的API中发布一个虚拟的方法,我们
不仅承诺,当你调用
这个方法,x和y会发生。我们
还承诺,在覆盖
这种方法,我们将它称为这个
特定顺序对于
这些其他的,国家将
在这个和那个不变的。
When we make something virtual in a platform, we're making an awful lot of promises about how it evolves in the future. For a non-virtual method, we promise that when you call this method, x and y will happen. When we publish a virtual method in an API, we not only promise that when you call this method, x and y will happen. We also promise that when you override this method, we will call it in this particular sequence with regard to these other ones and the state will be in this and that invariant.
每一次你在一个API,$ b $说虚拟b中创建一个回调挂钩的时间。由于
操作系统或API框架设计,
你必须是真实的谨慎
说。你不希望用户覆盖
和在$ B $任意点B的API挂钩,因为你不一定能
做出这些承诺。而人们可以
不能完全理解他们
当他们的东西
虚拟正在的承诺。
Every time you say virtual in an API, you are creating a call back hook. As an OS or API framework designer, you've got to be real careful about that. You don't want users overriding and hooking at any arbitrary point in an API, because you cannot necessarily make those promises. And people may not fully understand the promises they are making when they make something virtual.
这篇关于为什么Java函数都写在默认情况下和C#非虚虚拟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!