Inheritance
The process of creating a new class (called a derived class or a subclass) on an existing class (called a base class or a parent class) is inherited. The derived class can automatically obtain the base class (All members except the constructor and destructor). You can add new attributes and methods in the derived class to extend its functions.
Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Public class Person
{
Private string _ id;
Public string id
{
Get {return _ id ;}
Set {_ id = value ;}
}
/// <Summary>
/// Constructor
/// </Summary>
/// <Param name = "id"> </param>
Public Person (string id)
{
_ Id = id;
}
/// <Summary>
/// No parameter Constructor
/// </Summary>
/// <Param name = "id"> </param>
/// <Param name = "Class"> </param>
Public Person ()
{
}
Public string GetID ()
{
Return id;
}
}
// <Access modifier> class derived class name: Base class Name
Public class Student: Person
{
Private string _ Class;
Public string Class
{
Get {return _ Class ;}
Set {_ Class = value ;}
}
/// <Summary>
/// No parameter Constructor
/// </Summary>
/// <Param name = "id"> </param>
/// <Param name = "Class"> </param>
Public Student ()
{
}
Public string GetClass ()
{
Return Class;
}
}
Public class NewStudent: Student
{
/// <Summary>
/// No parameter Constructor
/// </Summary>
/// <Param name = "id"> </param>
/// <Param name = "Class"> </param>
Public NewStudent ()
{
}
Private string _ NID;
Public string NID;
Public string GetNID ()
{
Return NID;
}
}
Copy codeThe Code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
Person p = new Person ("ID1111111 ");
Student s = new Student ();
NewStudent n = new NewStudent ();
S. id = "ID2222222 ";
S. Class = "12 Class ";
N. NID = "NID3333333 ";
N. id = "ID3333333 ";
N. Class = "Class 13 ";
Label1.Text = p. GetID ();
Label2.Text = s. GetID () + s. GetClass ();
Label3.Text = n. GetID () + s. GetClass () + n. GetNID ();
// Output result ID1111111111 ID222222212 ID333333312 NID3333333
}
From the example above, we can see that if C is derived from B, B is born from. C not only inherits members in B, but also members in. The singularity of inheritance means that a derived class can only inherit from one base class, and cannot inherit from multiple base classes at the same time. A derived class can only access members modified by public, protected, and internal in the base class.
The base keyword is used to call the constructors, attributes, and methods of the base class in the derived class.
Copy codeThe Code is as follows: public Student (string id): base (id) // call the constructor of the base class
{
}
Implementation of polymorphism (use of virtual override abstract)
In C # learning, it is easy to confuse the use of virtual and abstract methods. Now let's discuss the differences between them. Both of them involve the use of override in a derived class.
1. Virtual Method)
Virtual keywords are used to modify methods in the base class. Virtual instances can be used in two scenarios:
Case 1: The virtual method is defined in the base class, but the virtual method is not overwritten in the derived class. In the call to a derived class instance, the virtual method uses the method defined by the base class.
Case 2: The virtual method is defined in the base class, and then override is used in the derived class to override the method. In the call to a derived class instance, this virtual method uses a derived override method.
2. Abstract Method)
Abstract keywords can only be used to modify methods in abstract classes without specific implementation. Abstract methods must be implemented using the override keyword in the derived class.
Copy codeThe Code is as follows: public abstract class Person
{
Private string _ id;
Public string id
{
Get {return _ id ;}
Set {_ id = value ;}
}
/// <Summary>
/// Constructor
/// </Summary>
/// <Param name = "id"> </param>
Public Person (string id)
{
_ Id = id;
}
/// <Summary>
/// No parameter Constructor
/// </Summary>
/// <Param name = "id"> </param>
/// <Param name = "Class"> </param>
Public Person ()
{
}
Public virtual string GetID ()
{
Return "the virtual method can overwrite ";
}
Public virtual string GetID1 ()
{
Return "the virtual method can overwrite ";
}
Public string GetID2 ()
{
Return "the general method, which is rewritten in the derived class, must use new ";
}
Public abstract string GetID3 (); // abstract method. This method must be inherited without the subject derived class.
}
// <Access modifier> class derived class name: Base class Name
Public class Student: Person
{
Private string _ Class;
Public string Class
{
Get {return _ Class ;}
Set {_ Class = value ;}
}
Public Student ()
{
}
Public Student (string id): base (id) // call the constructor of the base class
{
}
Public override string GetID ()
{
Return "the virtual method can overwrite-override ";
}
Public new string GetID2 ()
{
Return "common method override-use new ";
}
Public override string GetID3 ()
{
Return "the abstract method must be implemented-override ";
}
}
Copy codeThe Code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
Var a = new Student ();
Label1.Text = a. GetID ();
Label2.Text = a. GetID1 ();
Label3.Text = a. GetID2 ();
Label4.Text = a. GetID3 ();
// Running result
// The virtual method can be overwritten.-The override virtual method can be used to overwrite the common method.-The new abstract method must be used.-override
}