Objective-c Five main features:
- Fully compatible with C
- Object oriented
- Single Inheritance (functions in OC are virtual functions, pseudo-Multiple inheritance through classes and protocols)
- Dynamic binding (when dynamic_cast converts a base pointer to a subclass pointer, it automatically determines that the ID type represents any type of object, enabling dynamic binding)
- Message mechanism (Smalltalk language resolves null pointer error crashes)
#import<Foundation/Foundation.h>//: Indicates public inheritance@interfaceperson:nsobject{//property, instance variable, default permission is protected intAge ; //Object declaration must be used *NSString *name;} //generating set and Get methods@property (nonatomic,assign)intAge ;//-Represents the instance's method (message), + represents the class's method (message) Static- (void) eat;
-(void) SetName: (NSString *) namevalue;
@end
#import "Person.h"@implementation Person@synthesizeAge ;- (void) eat{NSLog (@"eat function called"); }- (void) SetName: (NSString *) namevalue{if(namevalue) {//pointers cannot be assigned directly, otherwise problems may occur when released if(Name! =namevalue) {Name=Nil; Name=[NSString Stringwithstring:namevalue]; } }}@end
#import "Person.h"#import<Foundation/Foundation.h>intMainintargcConst Char*argv[]) {@autoreleasepool {//Alloc is the + method, init is-methodPerson * p =[[Person alloc]init]; [P eat]; //OC string must begin with @[P SetName:@"yangjing"]; } return 0;}
Objective-c 2.0