Abstract: Referring to object-oriented, we have to say three major characteristics of object-oriented, encapsulation; inheritance; polymorphism! The following is still a diagram and examples of the way to continue to understand the object-oriented three core!
The following is also the analysis by the specific diagram:
Use the code to feel the syntax:
classProgram {Static voidMain (string[] args) {Fu F1=NewFu (); F1. Getfu (); //The parent class can only get the property method of the parent class, and the method of the subclass cannot be obtained .Zi z1 =Newzi (); Z1. Getzi (); //subclasses can obtain both the method property of the child class and the properties and methods of the parent class .Z1. Getfu (); Console.readkey (); }} classFu { Public voidGetfu () {Console.WriteLine ("Parent class Method"); } } classZi:fu//C # differs from C + + in that it can only inherit single { Public voidGetzi () {Console.WriteLine ("sub-class methods"); } }
Take a look at the inherited relationship through the diagram:
Three. Polymorphism (one object can have multiple shapes):
1. Concept:refers to a name that can have multiple semantics. In object-oriented languages, polymorphism is a method in a class in an inheritance tree that can have multiple methods with the same name but different method bodies and different parameters.
- There are two ways to achieve polymorphism, overwrite, overload.
- Polymorphism allows us to think of a subclass object as a parent class object type
Example: Father A = new Child ()
2. Polymorphism is divided into two types:
- Compile-time polymorphic and run-time polymorphism.
compile-Time type: Defines the type (subjective concept) as what it is.
Runtime type: Real type (objective concept) what he actually is.
Overloading is also called compile-time polymorphism, and overlay is called run-time polymorphism.
3. Three principles of runtime polymorphism:
- The object is not changed (subjective knowledge is changed)
- Calls to objects can be limited to methods of compile-time types.
- When the program is running, the dynamic type is determined. The runtime invokes the run-time type, which means that he calls the overridden method.
Use the code to feel the syntax:
classProgram {Static voidMain (string[] args) {Fu F1=NewZi (); Console.WriteLine (F1); F1. Show (); Zi Z1=NewZi (); Console.WriteLine (z1); Z1. Show (); Console.readkey (); } }classFu { Public Virtual voidShow ()//static methods cannot be overridden.{Console.WriteLine ("methods of the parent class, which will be overridden in the child class"); } } classZi:fu { Public voidSayHello (stringname) {Console.WriteLine (name+"Hello"); } Public voidSayHello (stringNamestringMsg//the number of parameters is different, and the parameter type can be overloaded{Console.WriteLine (name+"Hello:"+msg); } Public Override voidShow ()//overriding the methods of the parent class{Console.WriteLine ("overriding the methods of the parent class"); } //Public new void Show ()//This method of Fu in the main function after using the New keyword and this method of Zi became two irrelevant methods//{ //Console.WriteLine ("Overriding the method of the parent class"); //}}
Note the point:
- Polymorphism is one of the important characteristics of object-oriented, which means that the same operation acts on different objects, can have different interpretations, and produces different execution results.
- new overrides are actually hidden from the parent class method, which can be called by the overridden parent class method. So the parent method that new can override (or hide) is not necessarily defined as a virtual or abstract method. Only if the parent class method is a virtual method or an abstract method overrides the parent class method, and if not, it is hidden.
- conditions for overloading and overwriting:
Overload,Must happen in a class.,Function name is the same,parameter types or different order form overloads,Regardless of return type
Rewrite,Must occur in base classes and derived classes,virtual decorate ,override modifier
Overrides non-virtual function ,new keywords new Modifier Hidden, the way in which the parent class can be hidden by new in a subclass
- New overrides differ from overrides, overloads:
When the child class is not the same as the parent class's argument
When a base class function is not a virtual function, the base class function is hidden. (not overloaded because subclasses and base classes are not in the same scope)
When a base class function is a virtual function, the base class function is hidden. (not overloaded because subclasses and base classes are not in the same scope; Because the parameters are different, they are not overridden)
When the child class is the same as the parent class's arguments
When a base class function is not a virtual function, the base class function is hidden. (because the subclass and base class are not in the same scope, so it is not overloaded because the base class is not a virtual function, so hiding is not overriding)
When a base class function is a virtual function, the base class function is overwritten. (not overloaded because subclasses and base classes are not in the same scope)
Object-oriented in C #-encapsulation, inheritance, polymorphism