C# Summary of Class Inheritance

C# Summary of Class Inheritance

1. Parent class and subclass (base class and derived class)

In class inheritance, the inherited class is called the base class (together with the derived class) or parent class (together with the subclass), and the inherited class is called the derived class or subclass. The subclass inherits the properties, methods, etc. of the parent class, but the subclass does not inherit the private fields of the parent class.

public class Person
{
    
    
    public string _name;
    private int _age;
    
    public void SayHello()
    {
    
    
        Console.WriteLine("大家好,我是{0},我是人类", _name);
    }
}

public class Student:Person
{
    
    

}

class Program
{
    
    
    static void Main(string[] args)
    {
    
    
        Student stu = new Student();
        stu._name = "李明";
        stu.SayHello();//输出:大家好,我是李明,我是人类
        Console.ReadKey();
    }
}

Subclass objects cannot access parent class private fields:

insert image description here

2. Inherited features

  1. single root

    子类只能继承一个父类,但一个父类可以有多个子类。所以C#不支持多重继承
    
  2. transitivity

    继承是可以传递的。子类不仅继承父类中声明的成员,也继承父类的父类中的声明成员。
    
    public class ancestor
    {
          
          
        public char _gender;
    }
    public class parent:ancestor
    {
          
          
        public string _name;
    }
    public class child:parent
    {
          
          
    
    }
    class Program
    {
          
          
        static void Main(string[] args)
        {
          
          
            child ch = new child();
            ch._name = "李华";
            ch._gender = '女';
        }
    }
    
  3. automatic hiding

    如果子类与父类存在相同的函数名,父类会自动隐藏该函数。因此子类调用与父类相同函数名的函数时,调用的是子类或者说是自己的函数。
    
    public class Person
    {
          
          
        public void SayHello()
        {
          
          
            Console.WriteLine("大家好,我是人类");
        }
    }
    
    public class Student:Person
    {
          
          
    	//如果有意隐藏,请使用关键字 new
        public new void SayHello()
        {
          
          
            Console.WriteLine("大家好,我是学生类");
        }
    }
    
    class Program
    {
          
          
        static void Main(string[] args)
        {
          
          
            Student stu = new Student();
            stu.SayHello();//输出:大家好,我是学生类
            Console.ReadKey();
        }
    }
    
    new的两个作用:
    
    1、创建类的对象
    2、隐藏从父类继承的同名成员,隐藏后子类调用不到父类成员
    

3. Call the constructor of the parent class

The subclass does not inherit the constructor of the parent class, but the subclass will call the parameterless constructor of the parent class by default. The purpose of calling the no-argument constructor of the parent class is to create an object of the parent class, so that the subclass can use the members of the parent class.

If you write a parameterized constructor in the parent class, the subclass cannot call the parameterless constructor, and an error will be reported.

insert image description here
Solution:

  1. Rewrite a parameterless constructor in the parent class

    public class Person
    {
          
          
        public string _name;
        public int _age;
        
        public Person()
        {
          
          
    
        }
        
        public Person(string name, int age)
        {
          
          
            _name = name;
            _age = age;
        }
    }
    
    public class Student:Person
    {
          
          
        
    }
    
    class Program
    {
          
          
        static void Main(string[] args)
        {
          
          
            Student stu = new Student();
            stu._name = "李华";
            stu._age = 18;
            Console.ReadKey();
        }
    }
    
  2. Use the keyword [base] in the subclass to display the call to the parent class constructor

    public class Person
    {
          
          
        public string _name;
        public int _age;
    
        public Person(string name, int age)
        {
          
          
            _name = name;
            _age = age;
        }
    }
    
    public class Student:Person
    {
          
          
        public Student(string name, int age):base(name, age)
        {
          
          
    
        }
    }
    
    class Program
    {
          
          
        static void Main(string[] args)
        {
          
          
            Student stu = new Student("李华", 18);
            Console.ReadKey();
        }
    }
    

Due to the limited energy of the author, some mistakes and omissions will inevitably appear in the article. Experts and netizens are welcome to criticize and correct me.

Guess you like

Origin blog.csdn.net/qq_46051312/article/details/123315660