我们可能会在一些类中,写一些重复的成员,我们可以将这些重复的成员,单独的封装到一个类中,作为这些类的父亲。
    Student Teacher Driver 子类 派生类
    Person 父类 基类
    子类继承了父类的属性和方法,但是子类并没有继承父类的私有字段。

    子类并有没继承父类的构造函数。但是子类会默认的调用父类无参数的构造函数,创建父类对象,让子类可以使用父类中的成员。
    所以在父类中重新写了一个有参数的构造函数之后,那个无参数的就被干掉了,子类就调用不到了。
    解决方法:
    1、在父类中重新写一个无参数的构造函数。
    2、在子类中显示的调用父类的构造函数,使用关键字:base()
    public test(sting s):base(s)——->把s这个参数传给父类的构造函数。

    —————————————————继承的特性———————————————————
    1、单根性
    2、传递性

    1. using System;
    2. namespace _059_面对对象继承_05_继承
    3. {
    4. class Program
    5. {
    6. static void Main(string[] args)
    7. {
    8. Student student = new Student("老六", 18, '男', 10086);
    9. student.Study();
    10. Console.WriteLine("我的名字是:{0},今年{1}岁了,是个{2}生,我的编号是{3}", student.Name, student.Age, student.Gender, student.Id);
    11. Console.ReadKey();
    12. }
    13. }
    14. }
    15. class Person
    16. {
    17. public Person(string name,int age,char gender)
    18. {
    19. this.Name = name;
    20. this.Age = age;
    21. this.Gender = gender;
    22. }
    23. private string _name;
    24. public string Name
    25. {
    26. get { return _name; }
    27. set { _name = value; }
    28. }
    29. private int _age;
    30. public int Age
    31. {
    32. get { return _age; }
    33. set { _age = value; }
    34. }
    35. private char _gender;
    36. public char Gender
    37. {
    38. get { return _gender; }
    39. set { _gender = value; }
    40. }
    41. }
    42. class Student : Person
    43. {
    44. public Student(string name, int age, char gender, int id) : base(name, age, gender)
    45. {
    46. //this.Name = name;
    47. //this.Age = age;
    48. //this.Gender = gender;
    49. this.Id = id;
    50. }
    51. private int _id;
    52. public int Id
    53. {
    54. get { return _id; }
    55. set { _id = value; }
    56. }
    57. public void Study()
    58. {
    59. Console.WriteLine("学习");
    60. }
    61. }
    62. class Teacher:Person
    63. {
    64. public Teacher(string name, int age, char gender, double salary) : base(name, age, gender)
    65. {
    66. //this.Name = name;
    67. //this.Age = age;
    68. //this.Gender = gender;
    69. this.Salary = salary;
    70. }
    71. private double _salary;
    72. public double Salary
    73. {
    74. get { return _salary; }
    75. set { _salary = value; }
    76. }
    77. public void Teach()
    78. {
    79. Console.WriteLine("讲课");
    80. }
    81. }
    82. class Driver:Person
    83. {
    84. public Driver(string name, int age, char gender, int driveTime) : base(name, age, gender)
    85. {
    86. //this.Name = name;
    87. //this.Age = age;
    88. //this.Gender = gender;
    89. this.DriveTime = driveTime;
    90. }
    91. private int _driveTime;
    92. public int DriveTime
    93. {
    94. get { return _driveTime; }
    95. set { _driveTime = value; }
    96. }
    97. public void Drive()
    98. {
    99. Console.WriteLine("开车");
    100. }
    101. }