C# 里氏替换原则

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace C03LSP
{
    class Program
    {
        static void Main(string[] args)
        {
            //里氏替换原则:LSP 子类可以替换父类的位置,并且程序的功能不受影响.
            //父类有的功能子类都有,所以不影响程序的功能
            //父类变量指向了1个子类对象.
            //当1个父类变量指向1个子类对象的时候 只能通过这个父类变量调用父类的成员.子类独有的成员无法调用.

            Person p = new Person();
            p.SayHi();//调用父类的
            Student s = new Student();
            s.SayHi();//调用子类的.
            //运行时绑定.
            Person p1 = new Student();
            p1.SayHi();//父类的. 
            p1.Age = 19;
            p1.Name = "小花";
            p1.SayHi();

             //子类变量不能指向1个父类对象.
            //必须要有继承关系 才可以使用强制转换.

            //如果1个父类变量指向的就是1个父类对象 将这个父类对象转换为子类对象的会报异常.
            //Student s0 = (Student)p;//会报错.
            //如果1个父类变量指向了1个子类对象 那么可以将这个父类变量转换为为子类对象.
            Person p = new Person();
            Student s = new Student();
            Person p1 = new Student();
            //Student s1 = (Student)p1;

            //is 判断变量是否是指定的类型
            bool b = p is Person; //
            b = p is Student; //因为 1个Person不一定是Student
            b = p is Object; //因为Person从Object继承
            b = p is Program; //如果没有继承关系 表达式的值永远都是false

            b = p1 is Person; //父类变量指向子类对象 父类变量本身是Person 指向的对象学生也是1个Person
            b = p1 is Student; //p1变量指向的对象本来就是1个Student 所以true
            b = p1 is Program; //如果没有继承关系 表达式的值永远都是false
            b = p1 is object;
            //首先判断变量是不是要转换的类型
            if (p1 is Student)
            {
                Student s2 = (Student)p1;
            }

            //as 转换.

           // Student s3 = (Student)p; //有可能会报异常  
            //as转换 如果转换成功 引用指向s4 如果转换失败 不报异常 返回null

            Student s4 = p as Student;

            s4.StuNo = "123";

            Console.WriteLine(b);


            // Program pr = new Program();
            //s1 = (Student)pr; 不行 因为他们没有继承关系.

            Console.ReadKey();
        }
    }
}

 

Person类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace C03LSP
{
    class Person
    {
        public int Age { get; set; }
        public string  Name { get; set; }

        private double height;

        public void SayHi()
        {
            Console.WriteLine("我是父类的方法");
        }


    }
}

 

Student类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace C03LSP
{
    class Student:Person
    {
        public string StuNo { get; set; }

        //public void SayHi(int num)
        //{
        //    Console.WriteLine("我是子类的方法。。。。。"+num);
        //}
        public void SayHi()
        {
            Console.WriteLine("我是子类的方法。。。。。");
        }
        public void SayHi1()
        {
            Console.WriteLine("我是子类的方法。。。。。");
        }
    }
}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值