C#里氏转换语法应用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 里氏转换
{
class Program
{
static void Main(string[] args)
{
Person[] pers = new Person[10];
Random r = new Random();
for (int i = 0; i < pers.Length; i++)
{
int rNumber = r.Next(1, 7);
switch (rNumber)
{
case 1: pers[i] = new Student();
break;
case 2: pers[i] = new Teacher();
break;
case 3: pers[i] = new MeiNv();
break;
case 4: pers[i] = new ShuaiGuo();
break;
case 5: pers[i] = new YeShou();
break;
case 6: pers[i] = new Person();
break;
}
}
for (int i = 0; i < pers.Length; i++)
{
if (pers[i] is Student)
{
((Student)pers[i]).StudentSayHello();
}
else if (pers[i] is Teacher)
{
((Teacher)pers[i]).TeacherSayHello();
}
else if(pers[i] is MeiNv)
{
((MeiNv)pers[i]).MeiNvSayHello();
}
else if (pers[i] is ShuaiGuo)
{
((ShuaiGuo)pers[i]).ShuaiGuoSayHello();
}
else if (pers[i] is YeShou)
{
((YeShou)pers[i]).YeShouSayHello();
}
else
{
pers[i].PersonSayHello();
}
// pers[i].PersonSayHello();
}
// string.Join()
Console.ReadKey();
}
}
public class Person
{
public void PersonSayHello()
{
Console.WriteLine("1");
}
}
public class Student : Person
{
public void StudentSayHello()
{
Console.WriteLine("11");
}
}
public class Teacher:Person
{
public void TeacherSayHello()
{
Console.WriteLine("111");
}
}
public class MeiNv : Person
{
public void MeiNvSayHello()
{
Console.WriteLine("1111");
}
}
public class ShuaiGuo : Person
{
public void ShuaiGuoSayHello()
{
Console.WriteLine("11111");
}
}
public class YeShou : Person
{
public void YeShouSayHello()
{
Console.WriteLine("111111");
}
}
}