11_里式转换

 1  class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5            /*
 6             里式转换:
 7             *1.子类可以赋值给父类(如果一个方法需要一个父类作为参数,我们可以传一个子类对象)
 8             *2.父类中装的是子类对象,则那么可以将这个父类强转为子类对象。
 9             */
10 
11             Person p = new Student();
12             if (p is Student)
13             {
14                 ((Student)p).StudentSayHello();
15             }
16             else
17             {
18                 Console.WriteLine("转换失败");
19             }
20         }
21     }
22 
23     //父类
24     public class Person
25     {
26         public void PersonSayHello()
27         {
28             Console.WriteLine("我是人类");
29         }
30 
31     }
32 
33     //子类
34     public class Student : Person
35     {
36         public void StudentSayHello()
37         {
38             Console.WriteLine("我是学生");
39         }
40     }
原文地址:https://www.cnblogs.com/ncy123/p/13040231.html