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.