在下面的代碼中,我將子類投射到基類中,但get類型顯示它仍然是子類的實例。這裏發生了什麼?將子類投射到基類後,類型仍然是子類
using System;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
Car c1 = new Car { m = "M", c = "C" };
Machine m1 = (Machine)c1;
Console.WriteLine(m1.GetType());//ConsoleApplication.Car
}
}
public class Machine
{
public string m;
}
public class Car : Machine
{
public string c;
}
}
如果基類是'abstract'會怎麼樣?那麼有一個基類的實例呢? –
@retailcoder確實,或者如果你將它鑄造到'interface'! – Blorgbeard