Tôi đang cố gắng hiểu cách thức hoạt động của kế thừa trong C#. Tôi đã viết đoạn mã sau:Về thừa kế C#
class Program
{
static void Main(string[] args)
{
Animal animal = new Dog();
animal.OverRideMe();
//animal.NewMethod();
Dog dog = (Dog)animal;
dog.OverRideMe();
dog.NewMethod();
Console.Read();
}
}
public abstract class Animal
{
public Animal()
{
Console.WriteLine("Base Constructor");
}
public virtual void OverRideMe()
{
Console.WriteLine("In Base Class's OverRideMe");
Console.Read();
}
}
public class Dog : Animal
{
public Dog()
{
Console.WriteLine("Derived Constructor");
}
public override void OverRideMe()
{
Console.WriteLine("In Derived Class's OverRideMe");
Console.Read();
}
public void NewMethod()
{
Console.WriteLine("In Derived Class's NewMethod");
Console.Read();
}
}
Các CIL (Common Intermediate Language) mã cho Main() trông giống như sau:
.method private hidebysig static
void Main (
string[] args
) cil managed
{
// Method begins at RVA 0x2050
// Code size 42 (0x2a)
.maxstack 1
.entrypoint
.locals init (
[0] class ConsoleApplication1.Animal animal,
[1] class ConsoleApplication1.Dog dog
)
IL_0000: nop
IL_0001: newobj instance void ConsoleApplication1.Dog::.ctor()
IL_0006: stloc.0
IL_0007: ldloc.0
IL_0008: callvirt instance void ConsoleApplication1.Animal::OverRideMe()
IL_000d: nop
IL_000e: ldloc.0
IL_000f: castclass ConsoleApplication1.Dog
IL_0014: stloc.1
IL_0015: ldloc.1
IL_0016: callvirt instance void ConsoleApplication1.Animal::OverRideMe()
IL_001b: nop
IL_001c: ldloc.1
IL_001d: callvirt instance void ConsoleApplication1.Dog::NewMethod()
IL_0022: nop
IL_0023: call int32 [mscorlib]System.Console::Read()
IL_0028: pop
IL_0029: ret
} // end of method Program::Main
Các dòng trong CIL được gây phiền hà cho tôi là:
IL_000f: castclass ConsoleApplication1.Dog
IL_0014: stloc.1
IL_0015: ldloc.1
IL_0016: callvirt instance void ConsoleApplication1.Animal::OverRideMe()
IL_001b: nop
IL_001c: ldloc.1
IL_001d: callvirt instance void ConsoleApplication1.Dog::NewMethod()
Sau castclass của động vật để Chó nhập mã thực thi dog.OverRideMe();. Đây được phiên dịch sang CIL như
IL_0016: callvirt dụ trống ConsoleApplication1.Animal :: OverRideMe()
tôi đã đúc các động vật đối tượng để Chó loại. Tại sao nên dog.OverRideMe(); được dịch sang câu lệnh trên trong CIL? Sản lượng của các mã trên là:
sản lượng này không có gì để làm với cơ sở lớp Animal nhưng CIL vẫn đặt một cuộc gọi đến nó.
Tôi không hiểu nhu cầu giảm bớt câu hỏi này. Ít nhất để lại một bình luận để tôi có thể cải thiện nó. – msiyer
Câu hỏi của bạn là tốt, và thẳng thắn là một câu hỏi hay. Bỏ qua những kẻ thù ghét, họ không thể kiểm soát trên trang web những ngày này IMO. – jason