zoukankan      html  css  js  c++  java
  • 面向对象之继承

    继承:代码重用    多态

    继承(是指类与类之间的关系。)

    继承(加上封装和多态性)是面向对象的编程的三个主要特性(也称为“支柱”)之一。 继承用于创建可重用、扩展和修改在其他类中定义的行为的新类。 其成员被继承的类称为“基类”,继承这些成员的类称为“派生类”。 派生类只能有一个直接基类。 但是,继承是可传递的。 如果 ClassB 派生出 ClassC,ClassA 派生出 ClassB,则 ClassC 会继承 ClassB 和 ClassA 中声明的成员。

    lParent Class→父类Child Class→子类
    lBase Class→基类、 Derived Class→派生类、
     

    继承两大特性:
    单根继承性:
    只能继承一个类
    传递性:
    SuperMan继承了object,这时,SuperMan类中就有了从object类中继承下来的4个方法ToString()、
    GetType()、GetHashCode()、Equals()。然后Person又继承了SuperMan,这时,Person会将SuperMan中的那4个方法再继承下来。由于那4个方法是在object中的,所以相当于Person类间接从Object类中继承下来了成员。这个就叫继承的传递性。

    c#任何一个类都继承自object
    如果一个类没有显示继承自任何类,则默认继承自object类。
    如果显示的指定了当前类继承自某个类,则将覆盖默认继承的object类。

     1 namespace _05继承中的构造函数问题
     2 {
     3     class Program
     4     {
     5         static void Main(string[] args)
     6         {
     7             Student stu = new Student("杨中科", 18, 100);
     8             Console.WriteLine(stu.Name);
     9             Console.WriteLine(stu.Age);
    10             Console.WriteLine(stu.Score);
    11             Console.ReadKey();
    12 
    13         }
    14     }
    15     class Person
    16     {
    17         ////修正错误方法1:在父类中增加一个无参数的构造函数。
    18         ////这时子类的构造函数就可以找到父类中的无参构造函数了。
    19         //public Person()
    20         //{
    21         //    Console.WriteLine("Person类中的无参数的构造函数。。。。");
    22         //}
    23 
    24         public Person(string name, int age)
    25         {
    26             this.Name = name;
    27             this.Age = age;
    28         }
    29 
    30         public Person(string name)
    31         {
    32             this.Name = name;
    33             this.Age = 0;
    34         }
    35 
    36         public string Name
    37         {
    38             get;
    39             set;
    40         }
    41         public int Age
    42         {
    43             get;
    44             set;
    45         }
    46     }
    47     //1.继承的时候,构造函数不能被继承。
    48     //2.字类的构造函数会默认去调用父类中的无参数的构造函数
    49     class Student : Person
    50     {
    51 
    52         //修正错误方法2:不修改父类,而是在子类的构造函数后面通过:base(),显示的去调用父类的某个构造函数。
    53         public Student(string name, int age, double score)
    54             : base(name, age) //base的作用1:在子类中调用父类的构造函数。
    55         {
    56             // this.Name = name;
    57             //this.Age = age;
    58             this.Score = score;
    59         }
    60         public double Score
    61         {
    62             get;
    63             set;
    64         }
    65     }
    66     class Teacher : Person
    67     {
    68         public Teacher(string name, int age, double salary)
    69             : base(name)
    70         {
    71             //this.Name = name;
    72             this.Age = age;
    73             this.Salary = salary;
    74         }
    75         public double Salary
    76         {
    77             get;
    78             set;
    79         }
    80     }
    81 }
    继承中构造函数的问题
  • 相关阅读:
    PAT 1038. 统计同成绩学生
    PAT 1037. 在霍格沃茨找零钱
    PAT 1036. 跟奥巴马一起编程
    PAT 1035. 插入与归并
    PAT 1034. 有理数四则运算
    PAT 1033.旧键盘打字
    [转载]信号处理基础知识——加窗windowing
    On the use of spectrogram function in matlab
    [转载]【MATLAB】pwelch函数的相关参数解释
    [转载]时频特性分析(Matlab)
  • 原文地址:https://www.cnblogs.com/kongbei2013/p/3273487.html
Copyright © 2011-2022 走看看