C#里氏转换以及练习

里氏转换:可以将子类赋值给父类,如果父类装有子类对象,可以将父类强制转换为子类


 //子类可以给父类赋值
            student s = new student();
            person a = new student();
            //如果父类中装的是子类的对象,那么可以将父类强制转换成子类
            student ss = (student)a;
            //is 表示类型转换,如果成功则返回一个true,
            if (a is student)
            {
               student sss = (student)a;
            }
            else { Console.WriteLine("转换失败"); }
            //as 表示类型转化,成功则返回相印对象,失败则返回null;
            student t = a as student;
            t.say();
            Console.ReadKey();
        
    
     public class person
    {
        public void sayhello()
        {
            Console.WriteLine("我是父类");
        }
    }
    public class student : person
    {
        public void say()
        {
            Console.WriteLine("我是学生");
        }
    }


    public class drive:personS
    {
        public void test()
        {
            Console.WriteLine("我是司机");
        }
    }








  class Program
    {
        static void Main(string[] args)
        {
            //用循环调用十个对象的打招呼的方法;
            //student s = new student();
            //person p = new person();
            //shuaiguo sg = new shuaiguo();
            person[] pers = new person[10];
            Random r = new Random();
            for (int i = 0; i < 10; i++)
            {
                int number=r.Next(1,7);//六种类型的对象
                switch (number)//将子类对象装到父类中
                {
                    case 1: pers[i]=new student();
                        break;
                    case 2: pers[i] = new person();
                        break;
                    case 3: pers[i] = new teacher(); break;
                    case 4: pers[i] = new yeshou(); break;
                    case 5: pers[i] = new meilv(); break;
                    case 6: pers[i] = new shuaiguo(); break;
                  
                }
            }
            for (int i = 0; i < pers.Length; i++)//调用数组中每个子类元素的打招呼的方法
            {
                if (pers[i] is student)
                {
                  ((student) pers[i]).studentsayhi();
                }
                else if (pers[i] is teacher)
                {
                    ((teacher)pers[i]).teachsayhi();
                }
                else if (pers[i] is shuaiguo)
                {
                    ((shuaiguo)pers[i]).shuaiguosayhi();
                }
                else if (pers[i] is meilv)
                {
                    ((meilv)pers[i]).meilvsayhi();
                }
                else if (pers[i] is person)
                {
                    ((person)pers[i]).personsayhi();
                }
            }
            Console.ReadKey();
        }
    }
    public class person//父类
    {
        public void personsayhi()
        {
            Console.WriteLine("我是人类");
        }
    }
    public class student : person
    
    {
        public void studentsayhi()
        {
            Console.WriteLine("我是学生");
        }
    }
    public class teacher : person
    {
        public void teachsayhi()
        {
            Console.WriteLine("我是老师");
        }
    }
    public class meilv : person
    {
        public void meilvsayhi()
        {
            Console.WriteLine("我是镁铝");
        }
    }
    public class shuaiguo : person
    {
        public void shuaiguosayhi()
        {
            Console.WriteLine("我是帅哥");
        }
    }
    public class yeshou : person
    {
        public void yashousayhi()
        {
            Console.WriteLine("我是野兽");
        }
    }


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值