面向对象--里氏转换练习

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

namespace winformjicheng
{
class Program
{
static void Main(string[] args)
{
student stu = new student("张飞", "001", "男", 80);
stu.studentsayscore();
program1 pro = new program1("王五", "008", "男", 7);
pro.program1say();


//里氏转换1 子类可以赋值给父类:如果有一个地方需要一个父类对象,可以用一个子类对象代替;
string str=string.Join(",", new string[] { "2", "2", "2" });
Console.WriteLine(str);
//里氏转换 2 如果父类中装的是子类对象,可以将父类转换成子类
person pe = stu;
//student ss = (student)pe;
// ss.studentsayscore();
// Console.ReadKey();
//里氏转换--is 用法:(个人理解:pe可以转换为student 类么。可以的话就执行下面语句)
if (pe is student)
{
student ss = (student)pe;
ss.studentsayscore();
Console.ReadKey();

}
else
Console.WriteLine("转换失败");
//里氏转化--as 用法 (pe 可以转化为student 类么,可以的话直接转化完成输出sss 负责输出NUll )
student sss = pe as student;
sss.studentsayscore();
Console.ReadKey();

}

}
}

原文地址:https://www.cnblogs.com/woniu-net/p/4656182.html