Regain the polymorphism analysis in objective-c and the difference between overrides and overloads

Source: Internet
Author: User
Tags constructor inheritance
what is polymorphic.

The three major characteristics of object-oriented language, inheritance, encapsulation and polymorphism. Where encapsulation and inheritance are easy to understand, inheritance and polymorphism are the two attributes that complement each other. What is polymorphic. is the object-oriented language in the same interface can have different implementations, OC is different objects to the same message different responses, subclasses by overriding the parent class method to change the implementation of the same method, to reflect the polymorphism. In addition, we know that the polymorphism in C + + is mainly implemented by the Virtual keyword (virtual function, abstract class, etc.), which refers to allowing the parent class pointer to point to the subclass object and become a more generalized, more accommodating parent object. This allows the parent object to invoke the implementation of different subclasses of the parent-like interface, depending on which seed class object is actually.

As a simple example, there is a parent class people and two subclass man and women, the parent class has a default method Workhard, and two subclasses have their own rewrite implementations.

Parent class

@interface people:nsobject

-(void) Workhard;

@end
@implementation People

-(void) workhard{
    NSLog (@ "people work .... ");
}

@end

Sub-class

@implementation man

//overriding parent class
-(void) workhard{
    NSLog (@ "man make Money");
}

@end


@implementation Women

//overriding parent class
-(void) workhard{
    NSLog (@ "Women look after Children");
}

@end

Polymorphic Test Results:

People parent class pointer to people parent class
    people *p1 = [[People alloc] init];
    The people parent pointer to the man subclass
    people *p2 = [[Man alloc] init];
    Pointer to people parent class of women subclass
    people *p3 = [[Women alloc] init];
    
    [P1 Workhard]; People work ....
    [P2 Workhard];
    [P3 Workhard]; Women look after children

The actual project allows the controller to do this series of inheritance, through the basic logic of Baseviewcontroller, inheriting different subclasses of the same method of different implementations. are there overloads in Q1:objective-c and Swift?

There are overloads in Swift, but overloading is not supported in Objective-c.

First of all, the OBJECTIVE-C does not fully support overloading, many people on the web will either overload and rewrite to confuse, or OC does not support overloading (of course, according to the strict definition of overloading, OC does not support overloading is correct), in fact, OC supports a different number of function overloads.

Overloading: The function name is the same, the parameter list of the function is different (including the number of parameters and the parameter type), and the return type can be different. Overloading occurs between different functions of the same class and is horizontal. Overloading is independent of polymorphism.
Function name is the same, parameter name/parameter type/parameter number of different overloaded functions are not limited to constructor function overloading is an important sign of the face Object programming language OC does not support function overloading, OC is the alternative way is withxxx ... So some places say that OC does not fully support overloading, because different parameter names can cause the method name to change naturally, it can be said that OC does not support overloading

rewrite: Refers to the virtual function of the rewrite, to reflect polymorphism, refers to the subclass does not want to inherit the use of the parent class method, by overriding the implementation of the same function to implement the same function in the parent class overrides, so called function overrides. The overridden function must be identical to the parent class, including the function name, the number and type of arguments, and the return value, just overriding the function's implementation. Overrides occur between the parent and child classes, and are portrait-wise.

Also called overwrite, which defines a method in a subclass that has the same name as the argument list of a method in the parent class. Overriding is a method of a subclass that overrides a method of the parent class, requiring the method name and argument to be the same because the subclass inherits the method of the parent class, and the override is to redefine the method from the parent class and re-fill the code in the method. Overrides must inherit (portrait), overloaded without (landscape)

Swift is a new language optimized based on the C and OC languages, is free of C's compatibility restrictions, uses a secure programming model and adds new features to make programming more fun, friendly, and adaptable to the trends and expectations of language development. function overloading is supported as a part of polymorphism in Swift, and may also be considered to compensate for the drawback of not fully supporting function overloading in OC. OC does not fully support overloading, because OC learners should find that the same class does not allow two functions that define the same function name and have the same number of arguments, regardless of the type of the parameter and the return value. But it is absolutely not supported at all, because OC allows to define two functions with the same name but different number of arguments, which means that OC supports function overloads with different number of parameters.

-(void) test: (int) one;
-(void) test: (int) One and both: (int.)
-(void) test: (int) One {
    NSLog (@ "one parameter!");
}


-(void) test: (int) One or both: (int)-{
    NSLog (@ "parameters!");
}
[Self test:1];          Output:one parameter!
[Self test:1 two:2];    Output:two parameter!

Although it is easy to think of multiple parameters of overloading, but strictly speaking, the method name is changed according to the parameters, so it can be said that not fully supported or not support overloading.

-(void) test: (int) one;
-(int) test: (float) one; Duplicate declaration of method ' test '
If the method name parameter is the same, this is a compiled


Swift Example

Class Person:nsobject {

    var name:string//Name
    var age:int     //Age

    //constructor
    init (name:string,age:int) {
        Self.name = name
        self.age  = Age
        //must initialize the object before Super.init ()
        super.init ()
    }
}
Inheritance Overrides
Class Student:person {

    var lesson:string//

    rewrite/
    /////////-Parameters:/   /-Name: Name
    ///< c7/>-Age:
    override Init (name:string, age:int) {

        lesson = "Python"//Must be placed before

        Super.init () Super.init (Name:name, age:age)

    }
}

Overriding instantiated objects let
    s = Student (name: "Joyce", age:18)
    print (S.lesson)//Python
to add a property to a method by overloading it

Parameter names, the parameters are incremented

Class Student:person {

    var lesson:string///
    //////////-Parameters:/   /-Name: Name 
  ///   -Age:  ages   //-Lesson: Course
    init (name:string, Age:int, lesson:string) {

        Self.lesson = Lesson

        super.init (Name:name, age:age)
    }

}
Overloaded instantiated objects let
    s1 = Student (name: "Handsome", age:21, Lesson: "HTML 5")
    print (S1.lesson)//HTML 5  

Summary:Overloading allows you to quickly add new properties to a method, which can be overridden by an external pass-through (many libraries are overloaded with many methods), can only be set within a method, and cannot be visualized externally by a class's parameter list (vertical inheritance for polymorphism)

the Q2.object-c class can inherit multiple. can implement multiple interfaces. Rewrite a class in a way that inherits well or classifies well. Why.

The Objective-c class only supports single inheritance, and cannot be multiple inheritance. The Protocol proxy protocol can be used to implement multiple interfaces by implementing multiple interfaces like C + + multiple inheritance, and in Objective-c, the polymorphic features are implemented by protocol protocols or category categories. The interface functions defined by the Protocol protocol can be implemented by multiple classes, which can be overridden or extended without changing the original class.

The general classification is better, because the category to rewrite the method of the class, only valid for this category, will not affect the other classes and the relationship between the original class


Q3. Is there a concept of virtual base class in cocoa? How concise the implementation.

There is no concept of virtual base class in cocoa, virtual base class is in C + + in order to solve the problem of multiple inheritance ambiguity, but only single inheritance in OC, to realize the multi-inheritance similar to C + +, can be implemented by Protocal protocol, because a class can implement multiple protocols, Similar to a class in Java, you can implement multiple interfaces.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.