C#里氏转换语法应用

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");
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宋小童

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值