Objective-C class inheritance method override method overload

Source: Internet
Author: User

I. Inheritance of Classes

In objective-C, class inheritance is similar to that in C ++. The difference is that objective-C does not support multiple inheritance. A class can only have one parent class, single inheritance makes the inheritance relationship of objective-C simple and easy to manageProgram.
In objective-C, the parent class of all classes is nsobject.

Example of class inheritance in objective-C:

@ Interface person: nsobject {nsstring * Name; int age;}-(nsstring *) Name;-(INT) age;-(void) setname: (nsstring *) input; -(void) setage: (INT) input; @ end;

Ii. Method Rewriting

In objective-C, subclass can inherit the methods in the parent class, instead of re-writing the same method, you can directly use the methods of the parent class.
But sometimes we don't want to use the parent class method, but want to make some changes. What should we do? You can overwrite the methods of the parent class by writing a method name, return type, and parameter in the subclass that has the same name as the parent class. Relatively simple.

Iii. Method Overloading

In objective-C, methods cannot be overloaded. That is to say, we cannot define two methods in the class: they have the same name, the same number of parameters, different parameter types, and different return value types. Otherwise, xcode reports an error.
The method defined below is incorrect:

-(Void) setx :( INT) intx;-(void) setx :( double) doublex;

This part is much simpler than C ++.

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.