inheritance, encapsulation, and polymorphism are important features of object-oriented programming .
The class whose members are inherited is called the base class , also called the parent class , and the class that inherits its members is called the derived class , also called a subclass .
Derived classes implicitly obtain all members of the base class except constructors and destructors .
derived classes can have only one direct base class, so C # does not support multiple inheritance , but a base class can have multiple direct derived classes.
inheritance can be passed .
That
if ClassB derives from ClassC,ClassA derived from ClassB,The ClassC (grandchild) inherits the members declared in the ClassB (child) and ClassA (parent).
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacetestclass{classProgram {Static voidMain (string[] args) {Son Son1=Newson (); Son1.minus (9,6); Console.read (); } //Parent Class Public classFather { Public voidSum (int_a,int_b,string _identity) { intsum = _a +_b; System.Console.WriteLine ("I am the {3} class, I calculated the result is: {0}, I calculate is {1}+{2}", Sum, _a, _b, _identity); } } //subclass subclass inherits elements of parent class Public classSon:father { Public voidMinus (int_i,int_j) { This. Sum (Ten,Ten,"Parent"); This. Sum (_i,_j,"Child"); } } }}
I am the parent class, and the result I calculated is: 20, I calculate the 10+10
I am a subclass, and the result I calculated is: 15, I calculated the 9+6
When the base class sum () method is private, does the derived class also inherit the method?
If the method is not found in Class B, is it not inherited? not really, private members have actually been inherited,
However, they cannot be accessed because private members can only be declared in their class or struct, so they do not appear to be inherited.
If we want to reduce the basic access, we can define the base class sum () method as protected.
Is it possible to prevent a class from being inherited by another class?
The answer is yes, C # provides a sealed modifier that prevents other classes from inheriting from the class.
classProgram {Static voidMain (string[] args) {Son Son1=Newson (); Son1.minus (9,6); Console.read (); } //Parent Class Sealed classFather { Public voidSum (int_a,int_b,string _identity) { intsum = _a +_b; System.Console.WriteLine ("I am the {3} class, I calculated the result is: {0}, I calculate is {1}+{2}", Sum, _a, _b, _identity); } } //subclass subclass inherits elements of parent class Public class son:father { Public voidMinus (int_i,int_j) { This . Sum (Ten, " father " )); //This three will be an error .This . Sum (_i,_j," sub " ); } } }
Description of C # inheritance