Objective-C basics 1: class inheritance and combination in OC, objective-coc

Source: Internet
Author: User
Tags uppercase letter

Objective-C basics 1: class inheritance and combination in OC, objective-coc

1. Class Definition and Declaration

The class declaration in OC ends with @ interface and @ end.

The class definition in OC starts with @ implementation and ends with @ end.

Method declaration in OC:

-(Void) setName: (NSString *) strName; the short line above-indicates this method, (void) indicates the return value, setName indicates the method name, (NSString *) strName indicates that the parameter is of the NSString * type and the name is strName. Note: () You must add

The member variables in OC are defined in {} of the class declaration. This is different from the method declaration. The method is outside {}.

Method for creating an object in OC: Animal * animal = [Animal new]; it indicates that a memory creation object is allocated to the Animal class by calling the new method.

Method call in OC: [animal setName: @ "akon"]; indicates that the animal method setName is called, and the input parameter is akon.

Note that method calls in OC are enclosed in brackets.

Good Habit of programming: the class name starts with an uppercase letter, and the method name and variable name start with a lowercase letter.

Example:

// Declare @ interface Animal: NSObject {@ privateNSString * strName;}-(void) setName: (NSString *) strName;-(NSString *) getName; @ end // define @ implementation Animal-(void) setName: (NSString *) strNameIn {strName = strNameIn;}-(NSString *) getName {return strName ;} @ endint main (int argc, const char * argv []) {Animal * animal = [Animal new]; [animal setName: @ "akon"]; NSString * strName = [animal getName]; NSLog (@ "name: % @", strName); return 0 ;}

 

2. class inheritance

In fact, the Animal in the above example demonstrates the inheritance of the class Animal class inherited from NSObject. To deepen the impression, create two classes Cat and Dog inherited from the parent class Animal. The Code is as follows:

@interface Animal : NSObject{@privateNSString* strName;}- (void) setName : (NSString*) strName;- (NSString*) getName;- (NSString*) manCallMe;@end@interface Cat : Animal@end@interface Dog : Animal@end#endif

The above Animal class defines a method manCallMe, which is intended to implement virtual functions similar to those in C ++. In C ++, virtual functions are used to implement dynamic binding.

So how does OC implement dynamic binding? As long as the subclass overrides the parent class method. For the code, see:

//// Animal. m // TestCons // Created by mac on 15/1/17. // Copyright (c) 2015 akon. all rights reserved. // # import <Foundation/Foundation. h> # import "Animal. h "@ implementation Animal-(void) setName: (NSString *) strNameIn {strName = strNameIn;}-(NSString *) getName {return strName;}-(NSString *) manCallMe {return @ "Animal" ;}@ end @ implementation Cat-(NSString *) manCallMe {return @ "Cat" ;}@ end @ implementation Dog-(NSString *) manCallMe {return @ "Dog";} @ end

When creating an object, save the object pointer as id. The Code is as follows:

int main(int argc, const char * argv[]) {        id animal[3];    animal[0] = [Animal new];    [animal[0] setName : @"a"];    animal[1] = [Cat new];    [animal[1] setName : @"b"];    animal[2] = [Dog new];    [animal[2] setName : @"c"];    for (int i = 0; i < 3; ++i) {        NSLog(@"getName:%@, manCallMe:%@", [animal[i] getName], [animal[i] manCallMe]);    }    return 0;}

Sometimes the subclass needs to call the method of the parent class. How to do this is similar to the C ++ _ super keyword. The OC has the super keyword, for example, calling the manCallMe method of Animal in Cat, do this:

[Super manCallMe]

 

3. Combination of Classes

Inheritance and combination are two methods of reusing classes. When should we use inheritance and combination? Inheritance emphasizes "a relationship". For example, a cat is an animal, and a cat is an animal. Then cats and dogs should inherit from animals. The combination emphasizes the relationship between ''hav''. For example, if an animal has legs and eyes, animals should combine legs and eyes.

The implementation of the combination is very simple, that is, simply entrusting the method call to the combined class. For example, the animal class has a method 'discharge ", so the implementation of this method can call the eye discharge method to achieve. Because it is very simple, the code will not be available.

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.