Object-oriented Three Treasures, inheritance, encapsulation and polymorphism.
Polymorphism is a basic part of object-oriented systems that are hard to understand. After listening to Jiang's lessons, I have some understanding of polymorphism, which is written here.
When talking about polymorphism, We Have To Say object-oriented. So what is object-oriented?
According to my understanding, object-oriented is a way to describe and encapsulate the world in which we live. Simply put, it is to map the world and all things are objects.
At this time, the greatness of object-oriented technology was revealed. What else can't be done for a method that can be mapped to the world?
Object-oriented means that we can see everything as an instance, and the name we give to these things is the abstraction of these things, that is, the class. The name of a general point is an abstract class, such as an animal.
It is the parent class inherited from the abstract class, such as the cat. The other specific point is to inherit the Child class of the parent class, such as the Persian cat. Note that until now, all classes we call are abstract classes (or classes that are not instantiated
You), this abstract class contains unusually complex attributes (each attribute is an enumeration variable) and methods that people can understand. Well, at this time, you can have a living self-Persian cat instance.
Now, let's assign a value to its name: "Xiaohua ".
As for the color of your eyes, the color of hair depends on the value assigned by God to its corresponding attributes. These attributes are enumeration variables, so there is no way, your little flowers have no personality.
Because the values of these enumerated variables are summed up based on people's experience. As for your small flower, you like bedwetting, And you can recognize the word or something, it depends on what interface you want it to implement. None of these belong to what you want to write today
So I skipped it.
The semantics of polymorphism seems complex. In fact, there are two methods. One is the hidden method (new) and the other is the override method (override ).
1. Hide Methods
The hidden method is to hide the same signature method of the parent class by using the new keyword in the subclass. After the subclass instance is assigned to the parent class instance, calling this method through a parent class instance actually calls the method implemented by the parent class.
Example:
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
namespace test
{< br> class
mybase
{< br>
Public void func ()
{
console. writeline ("I am a parent class method");
}< br> class
myderive: mybase
{< br>
Public new void func ()
{< br>
console. writeline ("I Am a subclass method");
}< BR >}
Class
Test
{
Static void main (string [] ARGs)
{
Mybase = new mybase ();
Mybase. func ();
Myderive = new myderive ();
Myderive. func ();
Mybase = myderive;
Mybase. func ();
Console. Readline ();
}
}
}
Output:
I am a parent class Method
Subclass Method
I am a parent class Method
2. Rewrite Method
The override method refers to rewriting the same signature method modified by the virtual keyword in the parent class by using the override keyword pair in the subclass.
When the subclass instance is assigned to the parent class instance, the method called by the parent class instance actually calls the method implemented by the subclass.
Example: note the two examplesCodeBut the results are quite different.
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
namespace test
{< br> class
mybase
{< br>
Public Virtual void func ()
{
console. writeline ("I am a parent class method");
}< br> class
myderive: mybase
{< br>
Public override void func ()
{< br>
console. writeline ("I Am a subclass method");
}< BR >}
Class
Test
{
Static void main (string [] ARGs)
{
Mybase = new mybase ();
Mybase. func ();
Myderive = new myderive ();
Myderive. func ();
Mybase = myderive;
Mybase. func ();
Console. Readline ();
}
}
}
Output:
I am a parent Function
I am a subclass function
I am a subclass function
You see, polymorphism is that simple.
Maybe I have to wonder about the use of polymorphism in programming. I will give another inappropriate example.
Don't all colleges start school now? We treat everyone in the university as a subclass inherited from the abstract class "people. These sub-classes are divided into student classes and
Instructor class.
Now, military training has started.
Who is training for military training? So we created a new instructor object "Xiao Zhang" and a group of student objects.
"Startup girls ".
Then, the military training first trained the station posture. At the beginning, the "leaders and youth" were all determined by their own temperament, that is, they realized the stand () method and stood casually. At this time, "Xiao Zhang" was unwilling to see it, so he said, "Everyone
". As a result, we started to stand up according to the original standing posture. After military training at noon, Xiao Zhang asked everyone to implement the Eat () method, that is, they all went to dinner.
Understanding
Which of the Eat () and stand () methods should be overwritten and hidden?
If I still don't understand it, I will write a pseudo-code section to explain the above vernacular.
Class person
{
Public Virtual void eat ();
Public void stand ()
{
// Standard Site Method
}
}
Class
Student: person, istudentable
{
Public new void stand ()
{
// Each person has a different standing posture
}
Public override void eat ()
{
// Different tastes for each user
}
}
Class
MASTER: person, imasterable
{
}
Class
University life
{
Public void military training ()
{
Master Profile = new master ();
Student [] toutiao.com = {"Zhang San", "Li Si", "Wang Wu "};
// When instructor John did not arrive, everyone would like to stand
For (INT I = 0; I <small heads. length, I ++)
{
Headers [I]. Stand ();
}
// Instructor John asked everyone to stand up.
Person [] Per = headers;
For (INT I = 0; I <per. length, I ++)
{
Per [I]. Stand ();
}
// At noon, instructor John said, "Everyone is going to dinner ."
Person [] per2 = young people;
For (INT I = 0; I <per2.length, I ++)
{
Per2 [I]. Eat ();
}
}
}