Inheritance of C# class

Inheritance of C# class

1. Parent and child classes (base and derived classes)

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

public class Person
{
    public string _name;
    private int _age;
    
    public void SayHello()
    {
        Console.WriteLine("Hello, I'm{0},I'm human", _name);
    }
}

public class Student:Person
{

}

class Program
{
    static void Main(string[] args)
    {
        Student stu = new Student();
        stu._name = "Li Ming";
        stu.SayHello();//Output: Hello, I'm Li Ming. I'm human
        Console.ReadKey();
    }
}

Subclass object cannot access parent private field:

2. Inherited properties

  1. Single root

    A subclass can inherit only one parent class, but a parent class can have multiple subclasses. therefore C#Multiple inheritance is not supported
    
  2. Transitivity

    Inheritance is transitive. Members of the parent class in the declaration inherit not only from members of the parent class in the declaration.
    
    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 = "Li Hua";
            ch._gender = 'female';
        }
    }
    
  3. Automatic concealment

    If the subclass has the same function name as the parent class, the parent class will automatically hide the function. Therefore, the function of the subclass is called.
    
    public class Person
    {
        public void SayHello()
        {
            Console.WriteLine("Hello, I'm human");
        }
    }
    
    public class Student:Person
    {
    	//If you intend to hide, use the keyword new
        public new void SayHello()
        {
            Console.WriteLine("Hello, I'm a student");
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            Student stu = new Student();
            stu.SayHello();//Output: Hello, I'm a student
            Console.ReadKey();
        }
    }
    
    new Two functions of:
    
    1,Create an object of class
    2,Hide the member with the same name inherited from the parent class. After hiding, the child class cannot call the parent class member
    

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 parent class parameterless constructor is called to create a parent object so that children can use members in the parent class.

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


terms of settlement:

  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 = "Li Hua";
            stu._age = 18;
            Console.ReadKey();
        }
    }
    
  2. Use the keyword [base] in the subclass to display and call the constructor of the parent class

    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("Li Hua", 18);
            Console.ReadKey();
        }
    }
    

4. Different roles of public, private and protected access modifiers in inheritance.

  1. public

    Public allows a class to expose its member variables and member functions to other functions and objects. Any public member of a parent class can be accessed by an external class or by a subclass.

    public class Person
    {
        public string name;
    }
    
    public class Student:Person
    {
        public void SayHello()
        {
            //The subclass function accesses the name field of the parent class
            Console.WriteLine("Hello, my name is{0}", name);
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            Person person = new Person();
            //Class to access the name field
            person.name = "Li Hua";
        }
    }
    
  2. private

    The private access modifier allows a class to hide its member variables and functions from other functions and objects. Only functions in the same class can access its private members, and subclasses cannot access the private members of the parent class. Even the instance object of the class cannot access its private members.

    public class Person
    {
        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; }//The same class can access private members
        }
    }
    
    public class Student:Person
    {
    	public Student()
        {
            Name = "Li Hua";
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            Person person = new Person();
            person.Name = "Li Hua";
        }
    }
    


  3. protected

    The protected access modifier allows a subclass to access the member variables and member functions of its parent class, but other classes cannot access the member variables and member functions, and even instances of a class cannot access its members.

    public class Person
    {
        protected string name;
    }
    
    public class Student:Person
    {
        public Student()
        {
            name = "Li Hua";
        }
    }
    

Because the author's energy is limited, some mistakes and omissions are inevitable in the article. Please criticize and correct them by experts and netizens.

Keywords: C# .NET microsoft visualstudio

Added by twister47 on Sun, 06 Mar 2022 17:29:39 +0200