Dark Horse programmer _ios Development _objective-c Learning Notes _ inheritance and classification

Source: Internet
Author: User

1. What is inheritance

Inheritance is one of the three main features of object-oriented programs, meaning that a new class is defined based on the basis of a class.

The inherited class becomes the parent class, and the new class becomes a subclass.

There are many practical examples of inheritance. For example, if a son inherits from his father, he inherits the property of his father.

For example, if the base class is person, then we can inherit the person class and produce a new subclass Student,teacher

Here the person is the parent class of the student class and the teacher class.

There are many benefits to introducing the concept of inheritance:

* Ability to extract duplicate code

* Establish a link between classes and classes

* Not change the original model based on the ability to expand the new method

inheritance in the 2.objective-c

@interface//personinherits the NSObject, obtains the NSObject class method;@end  @interface The/ /Student class inherits the person class @end@interface teacher:person   the //Teacher class inherits the person class @end

3. Attention to the use of programming

* The declaration of the parent class needs to precede the subclass, otherwise the subclass cannot recognize the undeclared statement

* Subclasses cannot have the same member variables as the parent class, but subclasses can override the original member methods of the parent class, such as overriding the Dealloc method

* When calling a member method, first go to the current class, if not found, go to the parent class to find

4.super

Super as the key word function:

* Call a method in the parent class directly, for example [Super Dealloc];

*super is in the object method, the object method of the parent class is called, and super is in the class method, then the class method of the parent class is called.

#import<Foundation/Foundation.h>@interfaceperson:nsobject{int_age;}- (void) Setage: (int) age;- (int) age;- (void) test;@end@implementation Person- (void) Setage: (int) age{ Self->_age =Age ;}- (int) age{returnSelf->_age;}- (void) test{//Self : Pointing to the caller of the direction, representing the current object    int_age = -; NSLog (@"Person 's age is%d years", self->_age);} @end intMain () { person*p = [PersonNew]; [P Setage:Ten];          [P test]; return 0;}
5. Classification

Why the classification of this mechanism in objective-c, then what are the benefits of classification?

For example, you have completed the code for a person class. The code is as follows:

#import <Foundation/Foundation> @interface person:nsobject-(void) run;//run-(void) eat;//eat @end

But after a while, we found that the person class not only needs run,eat function, but also can do jump and swim. Then it is common practice to add the Jump,swim method directly to the person class. Of course there is no problem here. However, in the actual development project, because the code is very large, and the logic is quite complex, we directly modify the source code before the risk of a great deal!

God says we have to classify, so we have a classification. Classification is a better choice when you simply want to extend some methods to the parent class based on the original parent class.

#import <foundation/foundation>#import "Person.h"//since it is an extension to the interface of the original class, it must include the original interface @interface person (jumpandswim)// Tells the compiler that this is the classification defined for person jumpandswim-(void) jump;//jump-(void) swim;//tour @end

So we can do it without modifying the original code, the extension of the method is done!

However, there are a few things to note about using classifications:

* Categories can access member variables in the original class, but you cannot add any member variables in the taxonomy. If you need to add member variables, consider creating subclasses

* The method name in the classification can be the same as a method name of the original class (that is, overloaded), but the same method of the original class will be invalidated and inaccessible.

* Can create a lot of categories

Dark Horse programmer _ios Development _objective-c Learning Notes _ inheritance and classification

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.