AI智能
改变未来

C#x学习.里式转换练习

  class Program
    {
        static void Main(string[] args)
        {
            Person[] per = new Person[10];//创建父类数组
            Random r = new Random();//随机数类
            for (int i = 0; i < 10; i++)
            {
                int num = r.Next(1, 7);//生成随机数
                switch (num)
                {
                    case 1:
                        per[i] = new Student();
                        break;
                    case 2:
                        per[i] = new Teacher();
                        break;
                    case 3:
                        per[i] = new MeiNv();
                        break;
                    case 4:
                        per[i] = new YeShou();
                        break;
                    case 5:
                        per[i] = new ShuaiGuo();
                        break;
                    case 6:
                        per[i] = new Driver();
                        break;
                }

                if (per[i] is Student)
                {
                    ((Student)per[i]).StudentSayHi();
                }
                else if (per[i] is Teacher)
                {
                    ((Teacher)per[i]).TeacherSayHi();
                }
                else if(per[i] is MeiNv)
                {
                    ((MeiNv)per[i]).MeiNvSayHi();
                }
                else if(per[i] is YeShou)
                {
                    ((YeShou)per[i]).YeShouSayHi();
                }
                else if(per[i] is ShuaiGuo)
                {
                    ((ShuaiGuo)per[i]).ShuaiGuoSayHi();
                }
                else if(per[i] is Driver)
                {
                    ((Driver)per[i]).DriverSayHi();
                }
            }
            

            Console.ReadLine();
        }
    }

    public class Person//父类
    {
        public void PersonSayHi()
        {
            Console.WriteLine(\”我是人类\”);
        }
    }

    public class Student:Person//子类
    {
        public void StudentSayHi()
        {
            Console.WriteLine(\”我是学生\”);
        }
    }

    public class Teacher : Person//子类
    {
        public void TeacherSayHi()
        {
            Console.WriteLine(\”我是老师\”);
        }
    }

    public class MeiNv : Person//子类
    {
        public void MeiNvSayHi()
        {
            Console.WriteLine(\”我是美女\”);
        }
    }

    public class YeShou : Person//子类
    {
        public void YeShouSayHi()
        {
            Console.WriteLine(\”我是野兽\”);
        }
    }

    public class ShuaiGuo : Person//子类
    {
        public void ShuaiGuoSayHi()
        {
            Console.WriteLine(\”我是帅锅\”);
        }
    }

    public class Driver : Person//子类
    {
        public void DriverSayHi()
        {
            Console.WriteLine(\”我是司机\”);
        }
    }

 

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » C#x学习.里式转换练习