Overloading and rewriting in objective-c _ios

Source: Internet
Author: User

Objective-c Overloads and Overrides

First of all, overloading is not fully supported in objective-c, and many people on the web either confuse overloading with rewriting, or the OC does not support overloading (of course, according to the strict definition of overloading, OC does not support overloading), in fact OC supports function overloads with different number of parameters.

Question: Are there any overloads in Objective-c and Swift?

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

Expand

overloading, overriding, and hiding the definitions of the three in the programming language

overload (Overload): The function name is the same, the function's argument list is different (including the number of parameters and parameter types), as for the return type can be different. Overloading can occur between different functions of the same class, or between inheritance relationships of parent subclass subclasses, which occur when the parent class subclasses are separated from the rewrite area.

overriding (Override): occurs between a parent class and a subclass, meaning that a subclass does not want to inherit a method that uses the parent class, overriding the same function in the parent class by rewriting the implementation of the same function, and thus calling it a function overlay. Note that the overridden function must be exactly the same as the parent class, including the function name, the number and type of arguments, and the return value, but only the implementation of the function is overridden, which is the key to separating the overloaded zone.

shadowing: after the overload and the rewrite area are separated, it is possible to mix the two in front. Of course there is no hidden in OC, the typical C + + has, through the virtual function and parent-child class function overload between the distinction, this is no longer discussed here. Where overloads and overrides are for functions, and hidden except functions are also directed at member variables. Shadowing occurs between the parent class and the subclass, which refers to a function or variable with the same name as the parent class that is hidden in the subclass, where the function has the same name, regardless of whether the parameter is the same or not. A function or variable with the same name as the parent class in a subclass is not visible, but still exists in the parent class.

Swift is an improved new language based on the C and OC languages, getting rid of the C compatibility limit, adopting a secure programming model and adding new features to make programming more interesting, friendly and adaptable to the trends and expectations of language development. function overloading as a part of polymorphism is supported in swift and may also be considered to make up for the flaw in OC that does not fully support function overloading. OC does not fully support overloading, because OC learners should find that two functions with the same function name and number of parameters are not allowed in the same class, regardless of whether the parameter type and the return value type are the same. But it is absolutely not supported, because the OC allows you to define two functions with the same function name but different number of arguments, that is, the OC supports a function overload with a different number of arguments.

For example, we can define a function with a different number of two parameters in a class, which is distinguished by the number of arguments:

overloaded function Definitions:

-(void) test: (int) one;
-(void) test: (int) one andtwo: (int) two;

Overloaded function implementations:

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


-(void) test: (int) one andtwo: (int) Two {
  NSLog (@ "two parameters!");
}

Polymorphic calls:

[Self test:1]; Output:one parameter!
[Self test:1 andtwo:2]; Output:two parameter!

You can see that OC can implement the function overload by the number of parameters, but if the arguments are the same, they cannot be compiled regardless of whether the parameter and the return value type are the same. The following definitions cannot be compiled by Xcode:

-(void) test: (int) one;
-(int) test: (float) one; Duplicate declaration of method ' test '

Thank you for reading, I hope to help you, thank you for your support for this site!

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.