首页 > 代码库 > 01.里氏准换与using关键字

01.里氏准换与using关键字

using关键字有什么用?什么是IDisposable?

using可以声明namespace的引入,还可以实现非托管资源的释放,实现了IDisposiable的类在using中创建,using结束后会自动调用该对象的Dispose方法,释放资源。加分的补充回答:using其实等价于try……finally,用起来更方便。


里氏转换

(1).子类可以赋值给父类

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


得出结论:子类对象可以调用父类的成员,但是父类只能调用自己的成员.

注:子类可以转换成父类有什么意义?

有时候,有的地方需要一个父类对象,这个时候,我们可以使用这个父类的一个子类

来代替.(因为子类可以赋值给父类.)



对于里氏转换的判断

is   表示类型转换,如果能够转换成功,则返回一个true,否则返回一个false

is的使用方法:

  1. if (p1 is Tether)
  2. {
  3. Tether t1 = (Tether)p1;
  4. t1.TetherSayHello();
  5. }
  6. else
  7. {
  8. Console.WriteLine("转换失败");
  9. }

as  表示类型转换,如果能够转换转换成功,则返回对应的对象,否则返回一个null.

as的使用方法:

  1. Student s3 = p1 as Student;
  2. s3.StudentSayHello();


里氏转换的练习01:
  1. namespace _02.里氏装换的练习
  2. {
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. /*
  8. 自己定义一个父类Person,两个子类Teacher和Student类.
  9. Person写一个有参数的构造方法.
  10. */
  11. /*
  12. 自己试试,子类可以自动转父类,父类转子类要通过强转.
  13. 为了不出错,可以先通过is判断或用as转换.
  14. */
  15. Person p1;
  16. Student s1 = new Student("张三", 18, ‘男‘);
  17. if (s1 is Person) //使用is进行转换
  18. {
  19. p1 = s1;
  20. Console.WriteLine("转换成功");
  21. }
  22. else
  23. {
  24. Console.WriteLine("转换失败");
  25. }
  26. Person p2;
  27. Teacher t1 = new Teacher("小兰", 16, ‘女‘);
  28. p2 = t1 as Person; //使用as进行转换
  29. if (p2 != null)
  30. {
  31. Console.WriteLine("转换成功");
  32. }
  33. else
  34. {
  35. Console.WriteLine("转换失败");
  36. }
  37. Console.ReadKey();
  38. }
  39. }
  40. public class Person
  41. {
  42. public Person(string name, int age, char gender)
  43. {
  44. this.Name = name;
  45. this.Age = age;
  46. this.Gender = gender;
  47. }
  48. private string _name;
  49. public string Name
  50. {
  51. get
  52. {
  53. return _name;
  54. }
  55. set
  56. {
  57. _name = value;
  58. }
  59. } //Name属性定义结束
  60. public int Age
  61. {
  62. get
  63. {
  64. return _age;
  65. }
  66. set
  67. {
  68. _age = value;
  69. }
  70. } //Age属性定义结束
  71. public char Gender
  72. {
  73. get
  74. {
  75. return _gender;
  76. }
  77. set
  78. {
  79. _gender = value;
  80. }
  81. } //_gender属性定义结束
  82. private int _age;
  83. private char _gender;
  84. public void PersonSayHello()
  85. {
  86. Console.WriteLine("我是父类");
  87. }
  88. } //Person 类定义结束
  89. public class Student:Person
  90. {
  91. public Student(string name, int age, char gender):base(name,age, gender)
  92. {
  93. }
  94. public void StudentSayHello()
  95. {
  96. Console.WriteLine("我是学生");
  97. }
  98. }//Student 类定义结束
  99. public class Teacher : Person
  100. {
  101. public Teacher(string name, int age, char gender) : base(name, age, gender)
  102. {
  103. }
  104. public void TeacherSayHello()
  105. {
  106. Console.WriteLine("我是老师");
  107. }
  108. }
  109. }
技术分享
 
里氏转换的练习02:
  1. namespace _03.子类赋值给父类的练习01
  2. {
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. Person[] p1 = new Person[10];
  8. Random r = new Random();
  9. int random = 0; //保存随机数
  10. for (int i = 0; i < p1.Length; i++)
  11. {
  12. random=r.Next(1, 7); //从1`6随机产生一个数
  13. switch (random)
  14. {
  15. case 1:
  16. p1[i] = new Student();
  17. break;
  18. case 2:
  19. p1[i] = new Teacher();
  20. break;
  21. case 3:
  22. p1[i] = new MeiLv();
  23. break;
  24. case 4:
  25. p1[i] = new YeShou();
  26. break;
  27. case 5:
  28. p1[i] = new ShuaiGuo();
  29. break;
  30. case 6:
  31. p1[i] = new Person ();
  32. break;
  33. }
  34. }
  35. for (int i = 0; i < p1.Length; i++)
  36. {
  37. if (p1[i] is Student)
  38. {
  39. ((Student)p1[i]).StudentSayHello();
  40. }
  41. else if (p1[i] is Teacher)
  42. {
  43. ((Teacher)p1[i]).ThacherSayHello();
  44. }
  45. else if (p1[i] is MeiLv)
  46. {
  47. ((MeiLv)p1[i]).MeiLvSayHello();
  48. }
  49. else if (p1[i] is Person)
  50. {
  51. p1[i].PersonSayHello();
  52. }
  53. else if (p1[i] is YeShou)
  54. {
  55. ((YeShou)p1[i]).YeShouSayHello();
  56. }
  57. else if (p1[i] is ShuaiGuo)
  58. {
  59. ((ShuaiGuo)p1[i]).ShuaiGuoSayHello();
  60. }
  61. }
  62. Console.ReadKey();
  63. }
  64. }
  65. public class Person
  66. {
  67. public void PersonSayHello()
  68. {
  69. Console.WriteLine("我是人类");
  70. }
  71. }
  72. public class Student : Person
  73. {
  74. public void StudentSayHello()
  75. {
  76. Console.WriteLine("我是学生");
  77. }
  78. }
  79. public class Teacher : Person
  80. {
  81. public void ThacherSayHello()
  82. {
  83. Console.WriteLine("我是老师");
  84. }
  85. }
  86. public class YeShou : Person
  87. {
  88. public void YeShouSayHello()
  89. {
  90. Console.WriteLine("我是野兽");
  91. }
  92. }
  93. public class ShuaiGuo:Person
  94. {
  95. public void ShuaiGuoSayHello()
  96. {
  97. Console.WriteLine("我是帅锅");
  98. }
  99. }
  100. public class MeiLv:Person
  101. {
  102. public void MeiLvSayHello()
  103. {
  104. Console.WriteLine("我是美女");
  105. }
  106. }
  107. }



来自为知笔记(Wiz)


01.里氏准换与using关键字