OC class inheritance, method overrides, and overloading

Source: Internet
Author: User

I. Inheritance of classes

The inheritance of classes in objective-c is similar to C + +, except that OBJECTIVE-C does not support multiple inheritance, a class can have only one parent class, and single inheritance makes Objective-c's inheritance relationship simple and easy to administer.
The parent class of all classes in Objective-c is NSObject.

Examples of inheritance of classes in Objective-c:

@interface Person:nsobject {         nsstring* name;         int age;  }  -(nsstring*) name;  -(int) age;  -(void) SetName: (nsstring*) input;  -(void) Setage: (int) input;  @end;

Second, Method rewrite

In Objective-c, subclasses can inherit methods from the parent class without having to rewrite the same method, directly using the parent class's methods.
But sometimes we do not want to use the parent method, but want to make some changes, how to do? As long as you write a subclass with the same method name, return type, and arguments as the parent class, you can override overrides for the parent class's methods. relatively simple.

Third, method overloading

In Objective-c, methods are not overloaded. That is, we cannot define two methods in the class: their names are the same, the number of parameters is the same, the parameter types are different, and the return value types are different. Otherwise, Xcode will make an error.
The methods defined below are incorrect:

-(void) SetX: (int) intx;-(void) SetX: (double) DoubleX;

OC class inheritance, method overrides, and overloading

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.