Inheritance: The main implementation of inheritance code reuse, save development time
Inheritance in C # has the following characteristics
1. A derived class can overwrite an inherited member if it defines a new member with the same name as the inherited member.
2. transitivity. If a inherits B,b and inherits C, then a can inherit the members declared in B as well as the members of the Declarations in C (properties, methods ...).
3. Constructors and destructors cannot be inherited, others can be inherited. The ability to access the members of the base class is defined by the access modifier: Private: Only the class itself can be accessed. Protected: Classes and derived classes can be accessed. Internal: Only classes in the same project can be accessed. Protected Internal: A combination of Protected and Internal. Public: Full access.
4. Derived classes are extensions of the base class, and derived classes can define their own members in extra.
5.base can call a member method of the parent class, in addition to constructors and destructors
6.new: The expression on the variable and the method is relative to the inheritance, the parent child (subclass) 2 classes have a variable with the same name param if the param variable of the parent class and the Param variable of the subclass are not the same then the subclass needs to be preceded by a new one when declaring Indicates that the variable is not a parent class, such as public new string id= "2";
public class Person {
Protected string id = "1";
Protected string name = "Wzm";
Public person () {
Console.Write ("Person class \ n");
}
public virtual void GetInfo ()
{
Console.WriteLine ("Id:{0},name:{1}", Id, name);
}
}
public class Employee:person
{
Public Employee (): Base ()//Call the constructor of the parent class * *
{
}
private string id = "2";
public override void GetInfo ()
{
Base. GetInfo (); Call the GetInfo () method of the parent class
Console.WriteLine ("Employee ID: {0}", Id);
}
}
Class Program
{
static void Main (string[] args)
{
Employee E = new Employee ();
E.getinfo ();
}
}
C # 's understanding of inheritance