class Program
{
public class Per
{
public void Say()
{
Console.WriteLine("Per");
}
}
public class PerSOne:Per
{
public void Say()
{
Console.WriteLine("PerSOne");
}
}
public class PerSTwo:Per
{
public void Say()
{
Console.WriteLine("PerSTwo");
}
}
static void Main(string[] args)
{
//里式转换-类多态
Per[] per = new Per[3];
Random r = new Random();
int rs = 0;
for (int i = 0; i < per.Length; i++)
{
rs = r.Next(0, 2);
switch(rs)
{
case 0:per[i] = new PerSOne();
break;
case 1:per[i] = new PerSTwo();
break;
}
}
for (int i = 0; i < per.Length; i++)
{
if (per[i] is PerSOne)
{
((PerSOne)per[i]).Say();
}
else if (per[i] is PerSTwo)
{
((PerSTwo)per[i]).Say();
}
else
per[i].Say();
}
Console.ReadKey();
}
// 拆装箱的两种数据必须具有继承关系int-string非继承关系不会发生拆装箱