Polymorphism, override, overload (OVERLOAD)

xiaoxiao2021-03-06  33

Polymorphism override overload also translates Override to overload. Regarding the translation of Override and OverLoad, it seems to be uniform. More should be: override and overload 1. Overriding: Overriding perhaps, OverWriting is more appropriate, OVERLOAD overrides refer to the function of redefining the parent class in the subclass (C derived class), its function name, parameter column, and return value type must be in the same parent class. The overlay function is strict, the override function and the overwritable function are only the function body (part of the curly brackets). When the derived class object calls the same name, the subclass is automatically called, not the father. The covered function version in the class. such as

Code:

#Ruby Code Class Base Def M1 Puts "In Base" End Class Sub

We said the SUB class covers the M1 method 2 of the parent class. Overload: In the same class, the phenomenon of the method of multiple as the same name is the phenomenon between the OverLoad overload occurs in the same class. In C or Java, the method is generally the return type method name (parameter 1, parameter 2) to determine whether 2 methods are OverLoad, mainly referring to the method name, the parameters are different, the parameters are different, the number of parameters, the same Whether the type of parameters of the location is independent, and the name of the parameter (type) is independent (parameter type / number / order, different), and has nothing to return to the return type. The program will determine the functions you need to call, such as C or Java, which is OverLoad Void M1 (); Void M1 (int Arg); Void M1 (int Arg, char * x); in Ruby, There is no such overload

Code:

#Ruby Code Def Method_a (a) PUTS "Method_A (A) A.TO_S END DEF METHOD_A (A, B) PUTS" A, B) " A.TO_S " B.to_S END

Method_a ("a") # This sentence will be wrong because the definition of this method has been redefined by Method_a (A, B) to overwrite

Method_a ("a", "b")

Method_a ("a") This sentence will be wrong because the definition of this method has been redefined by Method_a (A, B) to overwrite the weak type and variable parameters of Ruby, making OverLoad not obvious. 3. Polymorphism as a polymorphism, I have not seen a definition of seeing a look. Some is to allow a pointer to the child class type to the parent class type, of course, there is no concept of pointer in Java. Sometimes it is also known as dynamic binding or evening binding or runtime binding, meaning which object is determined when it is time to compile. One of my feelings is that one of the parent class provides an interface (service) and then uses the specific implementation of subclasses when the call is called. This combination of Interface in Java should be comparative, but I am too lazy to write a few examples. Let's take a look at an example in Java without Interface: Code:

// java code public class base {void m1 () {system.out.println ("in base, m1");} void m2 () {system.out.println ("in base, m2");} public static Void main (String [] argv) {base b = new sub (); b.m1 (); b.m2 ();}} class sub fulls base {void m1 () {system.out.println ("in sub , M1 ");}}

In the main method, B declaration is a base type, and instantiate is an instance of a SUB class. The M2 method is not implemented in the SUB. Therefore, the M2 method of the base will be called, and the SUB Overwirte's M1 method of the parent class, so, B.M2 () will call the M1 method of the SUB class. Possible negligible points in C :

Code:

// C code #include class base {public: void m1 () {cout << "i am in base / n";} Virtual void M2 () {cout << "I am in base, Virtual / N ";}}; Class Sub: public base {public: void m1 () {cout <<" I am in sub / n ";} Virtual Void M2 () {cout <<" I am in sub, virtual / N ";}}; void fnm1 (base & b) {b.m1 ();} void fnm2 (Base & b) {b.m2 ();} int main (void) {SUB S; S.M1 (); s . m2 (); base b; b.m1 (); b.m2 (); base * bs = new sub (); bs-> m1 (); bs-> m2 (); delete BS; SUB S; fnm1 (s); fnm2 (s); return (0);

In C , the Virtual keyword (of course, Sub :: m2 () of Virtual is omitted) The resulting results are as follows: Code:

I am in sub I am in Sub, Virtual I am in base I am in sub, Virtual I am in base I am in sub, virtual

In FNM2 (Base & B), B is a Sub type. If this method is Virtual, the M2 of the SUB is called, otherwise, the BASE M2 is called. The weak type of Ruby does not exist this problem.

Code:

#Ruby Code

Class Base

DEF M1

PUTS "in base"

end

end

Class Sub

DEF M1

PUTS "in sub"

end

end

DEF FN (a)

A.m1

end

A = Sub.new

Fn (a)

B = base.new

Fn (b)

In short, polymorphism is an object-oriented basic feature, and OverLoad should not be an object-oriented property.

转载请注明原文地址:https://www.9cbs.com/read-64627.html

New Post(0)