1、里式转换
(1)子类可以赋值给父类
(2)如果父类中装的是子类对象,那么可以将这个父类强转为子类对象。
2、子类对象可以调用父类中的成员,但是父类对象永远都只能调用自己的成员.
image.png

is:表示类型转换,如果能够转换成功,则返回true否则返回false。
as:表示类型转换,如果能够转换,则返回对应的对象,否则返回null。

  1. using System;
  2. namespace _060_面对对象继承_06_里式转换
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. Student student = new Student();
  9. //1、子类可以赋值给父类:如果有一个地方需要一个父类作为参数,我们可以给一个子类代替。
  10. //Person person = student;
  11. //person.PersonSayHello();
  12. Person person = new Student();
  13. string s = string.Join("|", new string[] { "1", "2", "3", "4", "5" });//new string[]{} --> object 子类赋值给父类
  14. Console.WriteLine(s);
  15. //2、如果父类中装的是子类对象,那么可以将这个父类强转为子类对象
  16. //is的用法
  17. if (person is Student)
  18. {
  19. Student student1 = (Student)person;
  20. student1.StudentSayHello();
  21. }
  22. else
  23. {
  24. Console.WriteLine("转换失败");
  25. }
  26. //as的用法
  27. //Teacher teacher = person as Teacher; 转换失败 teacher中的值为null
  28. Student student2 = person as Student;
  29. student2.StudentSayHello();
  30. Console.ReadKey();
  31. }
  32. }
  33. }
  34. class Person
  35. {
  36. public void PersonSayHello()
  37. {
  38. Console.WriteLine("我是父类");
  39. }
  40. }
  41. class Teacher:Person
  42. {
  43. public void TeacherSayHello()
  44. {
  45. Console.WriteLine("我是老师");
  46. }
  47. }
  48. class Student:Person
  49. {
  50. public void StudentSayHello()
  51. {
  52. Console.WriteLine("我是学生");
  53. }
  54. }

练习:

  1. using System;
  2. namespace _061_里式转换练习
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. Person[] person = new Person[10];
  9. Random r = new Random();
  10. for (int i = 0; i < person.Length; i++)
  11. {
  12. int rNumber = r.Next(1, 7);
  13. switch (rNumber)
  14. {
  15. case 1:
  16. person[i] = new Student();
  17. ((Student)person[i]).StudentSayHi();
  18. break;
  19. case 2:
  20. person[i] = new Beast();
  21. ((Beast)person[i]).BeastSayHi();
  22. break;
  23. case 3:
  24. person[i] = new Beauty();
  25. ((Beauty)person[i]).BeautySayHi();
  26. break;
  27. case 4:
  28. person[i] = new Handsome();
  29. ((Handsome)person[i]).HandsomeSayHi();
  30. break;
  31. case 5:
  32. person[i] = new Teacher();
  33. ((Teacher)person[i]).TeacherSayHi();
  34. break;
  35. case 6:
  36. person[i] = new Person();
  37. person[i].PersonSayHi();
  38. break;
  39. default:
  40. break;
  41. }
  42. }
  43. //for (int i = 0; i < person.Length; i++)
  44. //{
  45. // //person[i].PersonSayHi();
  46. // if(person[i] is Student)
  47. // {
  48. // ((Student)person[i]).StudentSayHi();
  49. // }
  50. // else if(person[i] is Beast)
  51. // {
  52. // ((Beast)person[i]).BeastSayHi();
  53. // }
  54. // else if (person[i] is Beauty)
  55. // {
  56. // ((Beauty)person[i]).BeautySayHi();
  57. // }
  58. // else if (person[i] is Handsome)
  59. // {
  60. // ((Handsome)person[i]).HandsomeSayHi();
  61. // }
  62. // else if (person[i] is Teacher)
  63. // {
  64. // ((Teacher)person[i]).TeacherSayHi();
  65. // }
  66. // else
  67. // {
  68. // person[i].PersonSayHi();
  69. // }
  70. //}
  71. Console.ReadKey();
  72. }
  73. }
  74. }
  75. class Person
  76. {
  77. public void PersonSayHi()
  78. {
  79. Console.WriteLine("我是人类");
  80. }
  81. }
  82. class Student:Person
  83. {
  84. public void StudentSayHi()
  85. {
  86. Console.WriteLine("我是学生");
  87. }
  88. }
  89. class Teacher:Person
  90. {
  91. public void TeacherSayHi()
  92. {
  93. Console.WriteLine("我是老师");
  94. }
  95. }
  96. class Beauty:Person
  97. {
  98. public void BeautySayHi()
  99. {
  100. Console.WriteLine("我是美女");
  101. }
  102. }
  103. class Handsome:Person
  104. {
  105. public void HandsomeSayHi()
  106. {
  107. Console.WriteLine("我是帅哥");
  108. }
  109. }
  110. class Beast:Person
  111. {
  112. public void BeastSayHi()
  113. {
  114. Console.WriteLine("我是野兽");
  115. }
  116. }