首页 > 代码库 > 继承,is,as,多态

继承,is,as,多态

继承中的构造方法:
1、创建子类对象时,一定会先创建父类对象
2、如果调用的子类构造方法没有使用base,就会自动调用父类无参的构造方法,
   如果父类没有无参的构造方法就会报错
3、如果调用的子类构造方法使用了base,就会根据base后面的参数调用父类对应的构造方法

注意:如果类中有定义有参的构造方法,建议把无参的构造方法也写出来

里氏替换原理:子类对象能替换父类对象,父类对象不能替换子类对象

is:判断类型是否匹配、兼容
as:对引用类型进行类型转换(特点:转换失败不会报异常,而是返回一个null)
强制转换:转换失败会抛出异常

 

  /// <summary>
    /// 父类
    /// </summary>
    class Person
    {
        //姓名
        public string Name { get; set; }
        //年龄
        public int Age { get; set; }
        //职业
        public string Job { get; set; }

        //注意:如果类中有定义有参的构造方法,建议把无参的方法也写出来
        public Person() { Console.WriteLine("Person的无参构造方法被调用了!"); }

        public Person(string name, int age,string job)
        {
            this.Name = name;
            this.Age = age;
            this.Job = job;
            Console.WriteLine("Person带3个参数的构造方法被调用!");
        }

        public void Eat()
        {
            Console.WriteLine("吃饭");
        }

        public void Sleep()
        {
            Console.WriteLine("睡觉");
        }
    }

 

//子类

class Student:Person
    {
        //特长
        public string Special { get; set; }
        //学习
        public void Study()
        {
            Console.WriteLine("学习");
        }
    }

class Teacher:Person
    {
        //教龄
        public int TeachYear { get; set; }

        public Teacher()
        {
            Console.WriteLine("Teacher无参构造方法被调用!");
        }

        public Teacher(string name,int age,string job,int teachYear):base(name,age,job)
        {
            this.TeachYear = teachYear;
            Console.WriteLine("Teacher带四个参数的构造方法被调用!");
        }

        public new void Eat()
        {
            Console.WriteLine("老师吃饭");
        }

        public void Teach()
        {
            Console.WriteLine("教书");
        }
    }

//使用

 class Program
    {
        //继承中的构造方法
        //static void Main(string[] args)
        //{
        //    //Teacher teacher = new Teacher();
        //    Teacher teacher = new Teacher("张三",23,"程序员",2);
        //    teacher.Eat();
        //}

        //is:判断类型是否匹配、兼容
        //static void Main(string[] args)
        //{
        //    Student stu = new Student();
        //    Console.WriteLine(stu is Student);//true
        //    Console.WriteLine(stu is Teacher);//false
        //    Console.WriteLine(stu is Person);//true
        //}

        //里氏替换原理
        //static void Main(string[] args)
        //{
        //    Person p1 = new Person();
        //    Person p2 = new Teacher();//正确,子类对象替换父类对象
        //    Teacher t = new Teacher();
        //    Teacher t2 = new Person();//错误,父类对象不能替换子类对象
        //}

        //as:对引用类型进行类型转换(特点:转换失败不会报异常,而是返回一个null)
        static void Main(string[] args)
        {
            Person p = new Teacher();
            //Console.WriteLine(p.TeachYear);//出错,因为p无法访问TeachYear(因为编译器认为p为Person类型)
            Console.WriteLine((p as Teacher).TeachYear);//正确,因为p已经转换为了Teacher

            Console.WriteLine(p as Student);//转换失败,结果为null
            Console.WriteLine((Student)p);//转换失败,出现异常
        }
    }