Today we are going to talk about inheritance, to be honest today is also my first contact.
What is the concept of inheritance? Is that a class can inherit properties and methods (members) of another class
Inheritance is a very important feature in object-oriented programming.
Well, don't say much nonsense, cut into the following:
1. First we define a subclass and create two constructs for it: a non-parametric construct and a parametric construct
Defining an enumeration class
It has its own unique properties in the defined subclass:
Properties and constructs in the defined parent class:
In the Main method:
It is also important to note that when invoking a subclass's parameter construct, we want to call the subclass construct without invoking the parent class's specified construct using the base keyword, which calls the parent class's parameterless construct by default.
Add:
01.base represents the parent class object, if base (): Call the constructor of the parent class
02.base represents a method call, which does not require a parameter type.
03.base (The argument order is consistent with the parent class construction, and the variable name and subclass construction parameters are consistent)
2. Add a little access modifier here
What we know: Public Private protected
Below I draw a diagram to briefly describe (√ means yes, x means no)
Current class subclass other classes (program)
Private√xx
Protected√√x
Public√√√
Summary: We can clearly understand the three access modifiers to the class members access limit strength: private>protected>public
3.new sub-class underlying schematic diagram
I simply use a diagram to describe:
Describe in words:
1. Go to the sub-class structure, do not enter the construction body
2. Turn to the parent class and enter the parent constructor to execute
3. Turn back to subclass constructs, execute subclass constructs
4. Go to main, build child class object in memory
4. Inheritance also has two major characteristics of this we should not forget that is the uniqueness and transitivity
Single-sex refers to a subclass that has only one parent class
Transitivity is the ability to use the properties and methods of the parent class as long as there is an inheritance relationship with the parent class
Now let's talk about polymorphism.
1. What is polymorphism? Literally means multiple forms.
In a professional sense, it means that the same operation acts on different objects, and can have different interpretations, resulting in different execution effects.
The overloaded method We touch is also a way of polymorphism.
How to achieve polymorphism? Don't worry, I'll explain.
(1) Overriding the implementation method
Methods defined in the parent class, defined as virtual by the virtual keyword
Define your own methods in subclasses, decorate with the override keyword, and implement overrides to the methods of the parent class
(2) Define the parent class variable, initialize the parent class variable with the subclass variable
Do not feel abstract, in fact, I was just beginning to learn the same, below a small case:
Create a person parent class
public class Person
{
Public virtual void SayHello ()
{
Parent class-specific methods
Console.WriteLine ("Parent class greeting method");
}
}
Create a Korea subclass
public class Korea:person//here to note that it inherits from the person class
{
Public override void SayHello ()
{
Console.WriteLine ("Kim Hee Sun Greeting method");
}
}
Create a Chinese class
public class Chinese:person//here to note that it inherits from the person class
{
public override void SayHello ()
{
Console.WriteLine ("Hello! ");
}
}
Create a American class
public class American:person//here to note that it inherits from the person class
{
Public override void SayHello ()
{
Console.WriteLine ("Hello");
}
}
We've learned about generics before, and we're going to use generics to store
List<person> list=new list<person> ();
Chinese chinese=new Chinese ();
American usa=new American ();
Korea korea=new Korea ();
List. ADD (Chinese);
List. ADD (USA);
List. ADD (Korea);
Below we can use foreach to traverse
Way One:
foreach (person in list)
{
Person. SayHello ();
}
Way two:
foreach (person in list)
{
//Mode two: Do not use unified invocation
if (person is Chinese)
{
Chinese Chinese = Person as Chinese;
Chinese. SayHello ();
}
if (person is Korea)
{
Korea chinese= Person as korea;
korea. SayHello ();
}
if (person is American)
{
American chinese= person as American;
American. SayHello ();
}
}
Here I'll go through an example:
Called in the Main method
Do you see the two loops above? Do not understand also does not matter, after the Bolg we also will explain
OK, I will talk about here, I am also a beginner, just the teacher and his understanding of the text expressed in words, this is my own kind of growth!
Simple inheritance and polymorphism in C #