• 0105-里氏转换


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _06里氏转换
    {
        class Program
        {
            /*
             * is 返回布尔类型
             * as 转换成功返回对象,否则返回null
             */
            static void Main(string[] args)
            {
                //里氏转换(装箱和拆箱)
                Person p = new Teacher();
                if (p is Teacher)
                {
                    Teacher th = p as Teacher;
                    if (th != null)
                        th.TeacherSayHello();
                }
                Person p1 = new Student();
                if (p1 is Student)
                {
                    ((Student)p1).StudentSayHello();
                }
                //虚方法演示
                Employer[] em = {new Employer(), new Manager(), new Programer()};
                foreach (var e in em)
                {
                   e.SignIn(); //输出:员工9点打卡,经理8点打卡,程序员不打卡
                }
                Console.ReadKey();
            }
        }
    
        class Person
        {
            public void SayHello()
            {
                Console.WriteLine("我是人类");
            }
        }
    
        class Teacher : Person
        {
            public void TeacherSayHello()
            {
                Console.WriteLine("我是老师");
            }
        }
    
        class Student : Person
        {
            public void StudentSayHello()
            {
                Console.WriteLine("我是学生");
            }
        }
        //虚方法使用关键字virtual声明,子类使用override覆盖重写
        class Employer
        {
            public virtual void SignIn() {
                Console.WriteLine("员工9点打卡");
            }
        }
    
        class Manager : Employer
        {
            public override void SignIn()
            {
                Console.WriteLine("经理8点打卡");
            }
        }
        class Programer : Employer
        {
            public override void SignIn()
            {
                Console.WriteLine("程序员不打卡");
            }
        }
    }
    凡哥,别他妈吹牛逼了
  • 相关阅读:
    Loj #6307. 「雅礼国庆 2017 Day1」Clique
    bzoj 4457: 游戏任务
    Codeforces 375 D Tree and Queries
    Codeforces 837 E Vasya's Function
    [POI2008]BLO
    Codeforces 451 E Devu and Flowers
    洛谷 P4318 完全平方数
    [JSOI2016]反质数序列
    bzoj 4320: ShangHai2006 Homework
    bzoj4454 C Language Practice
  • 原文地址:https://www.cnblogs.com/sdlz/p/14690885.html
Copyright © 2020-2023  润新知