mẫu mã lấy từ MSDNtại sao Object.GetType() không phải là ảo?
public class Test {
public static void Main() {
MyBaseClass myBase = new MyBaseClass();
MyDerivedClass myDerived = new MyDerivedClass();
object o = myDerived;
MyBaseClass b = myDerived;
Console.WriteLine("mybase: Type is {0}", myBase.GetType());
Console.WriteLine("myDerived: Type is {0}", myDerived.GetType());
Console.WriteLine("object o = myDerived: Type is {0}", o.GetType());
Console.WriteLine("MyBaseClass b = myDerived: Type is {0}", b.GetType()); }}
/*
This code produces the following output.
mybase: Type is MyBaseClass
myDerived: Type is MyDerivedClass
object o = myDerived: Type is MyDerivedClass
MyBaseClass b = myDerived: Type is MyDerivedClass
*/
Vì vậy, nó sẽ là hợp lý để làm cho GetType() ảo ít nhất nó hoạt động như ảo? Ai có thể giải thích điều đó không? Và câu hỏi khác Có bất kỳ phương pháp nào khác trong NET framework có hành vi giống như GetType không?
và hợp lý như vậy! –
có. bạn đúng. nhưng bạn có thể quá tải phương thức công khai này trong lớp dẫn xuất)) – Arseny
@Arseny: Sau đó, nó không giống như GetType() như được định nghĩa trên kiểu đối tượng. –