次のコードがどのように最適化されているのだろうか。具体的には、仮想通話と直接通話に関するものです。私はすべてが最適化されていると思う方法についてコメントしましたが、それはちょうど推測です。コンパイラは密接なクラスによって実装された仮想メソッドを最適化する方法
public abstract class Super
{
public abstract void Foo();
public void FooUser()
{
Foo();
}
}
public class Child1 : Super
{
public override void Foo()
{
//doSomething
}
}
public class SealedChild : Super
{
public override void Foo()
{
//doSomething
}
}
class Program
{
void main()
{
Child1 child1 = new Child1();
child1.Foo(); //Virtual call?
child1.FooUser(); //Direct call and then a virtual call.
SealedChild sealedChild = new SealedChild();
sealedChild.Foo(); //Direct call?
sealedChild.FooUser();
/* Two options: either a direct call & then a virtual call
* Or: direct call with a parameter that has a function pointer to Foo, and then a direct call to foo.
*/
Super super = child1;
super.Foo(); //Virtual call.
super.FooUser(); //Virtual call then direct call.
}
}
"SealedClass"は実際には封印されていません。だからあなたのChild1クラスと変わりはありません。 –
C#?それは知るのを助けるだろう。 –