In the previous article, I popularized some concepts in some of the object-oriented features without in-depth code. Here we will make a more detailed summary through the Code:
1. Encapsulation
That is, the permission modifier should be as small as possible.
Class Person {
// Because the default permission is private, which can be omitted here
PrivateString name;
PrivateInt age;
PrivateChar gender;
// Defines public attributes to facilitate content modification in fields
Public string Name {
Set {
Name = value;
}
Get {
Return name;
}
}
Public int Age {
Set {
// Another feature of attributes, which can limit the value range when setting values
If (value> 0 & value <150 ){
Age = value; // The logic is correct.
}
}
Get {
Return age;
}
}
Public char Gender {
Set {
Gender = value;
}
Get {
Return gender;
}
}
// This method specifies that only classes that inherit this class can be used.
Protected void SelfIntroduction (){
Console. WriteLine ("My name is {0}. I am {1} years old, I am {2} People", name, age, gender );
}
}
2. Inheritance
C # Only single inheritance is supported, that is, one son can only have one father. However, this father can have many sons. Single inheritance is easier to understand than multi-inheritance, in order to make up for the lack of single inheritance, the interface appears;
// Define the class of a worker and inherit from the class above;
ClassWorker: Person{
// In this case, fields, attributes, and methods in the parent class are owned in the face-to-face derived class.
// We can also add something specific to the work human.
Public void Work (){
Console. WriteLine ("I am working ");
}
// The protected member of the parent class will also be inherited by the derived class
}
After the derived class inherits the base class, it can also overwrite the base class.VisualVirtual method. When a derived class is overwrittenOverrideKeyword;
Example:
In Person, modify SelfIntroduction to a virtual method. The derived class will re-write this method (the meaning of re-writing and overwrite is one );
Class Person {
// The previous code is omitted. I only write the modified Code.
Protected visual void SelfIntroduction (){
Console. WriteLine ("My name is {0}. I am {1} years old, I am {2} People", name, age, gender );
}
}
Overwrite this method in Worker
Class Worker: Person {
Public override void SelfIntroduction (){
Console. writeLine ("My name is {0}. I am {1} years old, I am {2} people, and I work as a worker", name, age, gender ); // Of course, a work field can be added here, which is omitted
}
}
3. Polymorphism
Polymorphism, based on inheritance; or use the above example to create a new class Program
Class Program {
Static void Main (string [] args ){
// Normally generated object
Person p = new Person (); // The base class's own variable corresponds to its own instance
P. Name = "jn ";
P. Age = 22;
P. Gender = 'M ';
P. SelfIntroduction (); // The result is conceivable.
// The derived class generates the object normally.
Worker w = new Worker ();
W. Name = "wjx ";
W. Age = 30;
W. Gender = 'M ';
W. SelfIntroduction (); // This is the result of the worker
// Here is the cause of polymorphism.
Person pw = new Worker ();
Pw. SelfIntroduction (); // displays the worker,
/*
The variables of the parent class point to the subclass instance. Even if the Person is an interface or an abstract class, this is acceptable. This is like a worker, which is labeled by a Person, it is essentially a worker. In this case, we use this variable as a member of the worker. Although this is the case, if this variable of the parent class represents the parent class, it cannot point to some unique members of the Child class;
If I previously defined this field in the work human, the pw variable here will not be able to access this field. Because the Person class does not have this field;
So there is a question of downward transformation;
*/
Worker ww = (Worker) pw;
Ww. SelfIntroduction (); // display the worker
/*
Are you very depressed at this time? Isn't it difficult for me to do this? Why is it so troublesome. This function is not very obvious in this program. If the base class is derived from a student class, or if there are many derived classes of the base class, then this is useful at this time, it is displayed.
For example, we can define a method to gather people of different occupations from my introduction. If there is no polymorphism, we need to create different methods based on people in different occupations. The worker class requires a method, the student class also requires, and the derived class is less, so we can do this. However, if there are thousands of derived classes, this is very unrealistic;
In this case, we can do this.
// Set the method parameters to the base classes of all derived classes
Public static void EveryoneIntroduction (Person p ){
// Determine at this time and then transform
If (p. GetType () = typeof (Worker )){
Worker w = (Worker) p;
W. SelfIntroduction ();
} Else if (p. GetType = typeof (Student )){
Student s = (Student) p;
S. SelfIntroduction ();
}
// Even if there are many derived classes, we can process them in the same method in a centralized manner. Here we use a keyword typeof and the GetType method in an Object. The former is used to obtain the type of a class, and the latter is used to obtain the type of an object. They return the same values, so we can compare the types through them;
}
*/
}
}