1. Inheritance allows reuse of existing classes to create new classes. Principle: A subclass derived from a class has all the public attributes of the class.
The base class for creating a new class is called the base class or parent class, and the newly created class is called the derived class or subclass.
2. inherit classes in C #
C # multi-inheritance is not supported. If a base class is not specified in the declaration, it is inherited from System. Object.
Using System; namespace BaseConsole {# region base class public class Person {private string _ name; private uint _ age; public void GetInfo () {Console. writeLine ("enter your name and age"); _ name = Console. readLine (); _ age = uint. parse (Console. readLine ();} public void DispInfo () {Console. writeLine ("Dear {0}, your age is {1}", _ name, _ age) ;}# endregion # region derived class public class Student: person {private string _ school; private Uint _ eng; private uint _ math; private uint _ sci; public void GetMarks () {Console. writeLine ("enter the school name"); _ school = Console. readLine (); Console. writeLine ("Enter the scores of English, mathematics, and natural science respectively. "); _ Eng = uint. parse (Console. readLine (); _ math = uint. parse (Console. readLine (); _ sci = uint. parse (Console. readLine (); Console. writeLine ("total score: {0}", _ eng + _ math + _ sci );}} # endregion public class Exercise {[STAThread] static void Main (string [] args) {Student objStudent = new Student (); objStudent. getInfo (); // access the base class member objStudent. dispInfo (); // access the base class member objStudent. getMarks (); Console. readKey ();}}}
Result:
3. Call the constructors of the base class
The constructor is used to instantiate the member fields of a class,
The base keyword is used to access base class members from a derived class. You can use the base keyword to call the base class constructor and cannot access the static methods of the base class.
Using System; namespace BaseConsole {public class Persons {public string _ name; public uint _ age; // The base class provides functions to initialize public Persons (string name, uint age) {this. _ name = name; this. _ age = age; Console. writeLine (_ name); Console. writeLine (age) ;}} public class Students: Persons {private uint _ id; // call the Persons constructor public Students (string name, uint age, uint id ): base (name, age) {this. _ id = id; Console. writeLine (_ id) ;}} public class Exercises {[STAThread] static void Main (string [] args) {Students objStudent = new Students ("dong", 22,001); Console. readKey ();}}}
4. Rewrite the method in C #
"Override" the base class method is to modify its implementation or rewrite it in the derived class.
Keyword override
The override keyword is used to modify the method. methods with the same name in the base class must be declared as virtual or abstract. The virtual keyword is added to indicate that the implementation of the override can be rewritten in the derived class.
The new, static, and virtual keywords cannot be used with the override access modifier.
Keyword new
The new access modifier is used to explicitly hide the members inherited from the base class. That is, if the names of the derived class members are the same as those of the base class members, the new access modifier recognizes the derived class members as a new member.
Using System; namespace BaseConsole {class Employees {public virtual void EmpInfo () {Console. writeLine ("This method shows employee information");} class DervEmployee: Employees {public override void EmpInfo () {base. empInfo (); Console. writeLine ("This method overrides the base method");} class Test {[STAThread] static void Main (string [] args) {DervEmployee objDervEmployee = new DervEmployee (); objDervEmployee. empInfo (); Employees objEmployees = objDervEmployee; objEmployees. empInfo (); Console. readKey ();}}}
Result:
5. abstract classes and abstract methods
An abstract class is a class that contains at least one abstract member (a method that has not yet been implemented). An abstract class cannot be instantiated. An abstract class is the basis of a derived class and is not implemented or partially implemented, these abstract classes are used to create a blueprint or template to derive other classes.
An abstract class can contain abstract methods and non-abstract methods. The purpose of an abstract method is to specify that a derived class must implement the behavior associated with this method. An abstract method is only implemented in a derived class.
You can use the override keyword to implement abstract methods in a derived class. The method that is declared and overwritten by override is called the override base class method. Its signature must be the same as that of the override method.
Using System; namespace BaseConsole {// abstract class ABC {public abstract void AFunc (); public void BFunc () {Console. WriteLine ("non-abstract method! ") ;}// Class Derv: ABC {public override void AFunc () {Console. WriteLine (" abstract method! ") ;}} Class AbstractTest {[STAThread] static void Main (string [] args) {Derv objB = new Derv (); objB. AFunc (); objB. BFunc (); Console. readKey ();}}}
Result:
6. Interface
The interface cannot contain any implementation methods. Each method of the interface must be implemented in a derived class.
The function of an interface is to specify that the class that implements this specific interface must implement all the members listed by this interface. Sometimes it can be seen as the "Mold" of the class"
When declaring an interface. Consider the following:
The interface subject is limited to declarations of methods, indexers, and attributes.
The interface cannot contain fields, constructors, and constants.
Interface members are implicitly exposed. If you explicitly specify the access level for them, a compilation error will occur.
The interface cannot implement any method, attribute, or indexer.
The interface name must always contain an uppercase letter "I"
Using System; namespace BaseConsole {public interface IPict {int DeleteImage (); void DisplayImage () ;}// derived from the public class MyImages interface: IPict {// Implementation of the first method public int DeleteImage () {Console. writeLine ("DeleteImage implementation! "); Return (5) ;}// Implementation of the second method public void DisplayImage () {Console. WriteLine (" DisplayName implementation! ") ;}} Class interfaceTest {[STAThread] static void Main (string [] args) {MyImages objM = new MyImages (); objM. displayImage (); int t = objM. deleteImage (); Console. writeLine (t); Console. readKey ();}}}
Result: