C#基础(15)——里氏转换、protected

1、里氏转换

1)子类可以赋值给父类,如果一个地方需要父类作为参数,可将子类放进去:

 string s = string.Join("|", new string[] { "1", "2", "3" });

PersonInfo p = new Reporter(“Ale”,23,”play”);

2)如果父类中装的是子类对象,那么可以将这个父类强转为对应子类对象

PersonInfo p = new Reporter(“Ale”,23,”play”);
Reporter rep = (Reporter)p;

2、类型转换抛异常处理

1)is:如果转换成功返回true
这里写图片描述

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

namespace 练习
{
    class Program
    {
        static void Main(string[] args)
        {
            PersonInfo p = new Reporter("Ale", 23, "play");
            if (p is Reporter)
            {

                Reporter rep = (Reporter)p;
                rep.Reorting();
            }
            else
            {
                Console.WriteLine("转换失败!");
            }

            Console.ReadKey();
        }
    }
}

2)as:如果哪个转换,则返回一个对象,否则为null
null:
这里写图片描述
success:
这里写图片描述

这里写图片描述

3、例子

这里写图片描述
代码冗余,后期可以采用多态

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

namespace 里氏转换
{
    class Program
    {
        static void Main(string[] args)
        {
            //Person person = new Person();
            //Student studengt = new Student();
            //Beauty beauty = new Beauty();
            //Teacher teacher = new Teacher();
            Random r = new Random();
            Person[] personArray = new Person[10];
            for (int i = 0; i < personArray.Length; i++)
            {
                int rNumber = r.Next(1, 6);
                switch (rNumber)//1-5
                {
                    case 1: personArray[i] = new Person();
                        break;
                    case 2: personArray[i] = new Student();
                        break;
                    case 3: personArray[i] = new Teacher();
                        break;
                    case 4: personArray[i] = new Beauty();
                        break;
                    case 5: personArray[i] = new Driver();
                        break;
                }
            }

            for (int i = 0; i < personArray.Length; i++)
            {
                if (personArray[i] is Driver)
                {
                    ((Driver)personArray[i]).DriverSay();
                }
                else if (personArray[i] is Teacher)
                {
                    ((Teacher)personArray[i]).TeacherSay();
                }
                else if (personArray[i] is Student)
                {
                    ((Student)personArray[i]).StudentSay();
                }
                else if (personArray[i] is Beauty)
                {
                    ((Beauty)personArray[i]).BeautySay();
                }
                else
                {
                    personArray[i].PersonSay();//注意一定要放最后,因为父类是personArray,都是调用自己的方法,放在第一位结果都是PersonSay
                }
            }

            Console.ReadKey();
        }
    }


    public class Person
    {
        public void PersonSay()
        {
            Console.WriteLine("我是人");
        }
    }

    public class Student:Person
    {
        public void StudentSay()
        {
            Console.WriteLine("我是学生");
        }
    }

    public class Beauty : Person
    {
        public void BeautySay()
        {
            Console.WriteLine("我是美女");
        }
    }

    public class Driver : Person
    {
        public void DriverSay()
        {
            Console.WriteLine("我是司机");
        }
    }

    public class Teacher:Person
    {
        public void TeacherSay()
        {
            Console.WriteLine("我是老师");
        }
    }
}

4、protected

protected权限比private权限高一些,继承的子类和其他类中能够访问到,如“_name”,但是main函数里不能够访问到。
这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

何以问天涯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值