目前移動開發熱火朝天,今天在家學習最近入手的《Objective-c基礎教程》,初步感受Objective-c與.Net差異很大,爲了更好的學習理解Objective-c,將以筆記的形式記錄學習的知識點,方便查閱。objective-c
在Objective-c中類分爲兩部分:聲明和實現,下面定義一個Person類,安全
Person.h:函數
#import <Foundation/Foundation.h> @interface Person : NSObject { //實例成員變量聲明 @private NSString *_firstName; @protected NSString *_lastName; @public NSInteger *_age; } //屬性聲明 @property (nonatomic,strong) NSString *firstName; @property (nonatomic,strong) NSString *lastName; @property (nonatomic,assign) NSInteger *age; //實例方法 -(id)initWithFirstName:(NSString *)firstName lastName:(NSString *)lastName age:(NSInteger *)age; -(void)printFullName; //類方法 +(void)breath; @end
Person.m學習
#import "Person.h" @implementation Person -(id)initWithFirstName:(NSString *)firstName lastName:(NSString *)lastName age:(NSInteger *)age { if(self = [super init]) { self.firstName = firstName; self.lastName = lastName; self.age = age; } return self; } -(NSString *)description { return [[NSString alloc]initWithFormat:@" firstName: %@,lastName:%@,age:%zd",self.firstName,self.lastName,self.age]; } +(void)breath { NSLog(@"people breathing"); } -(void)printFullName { NSLog(@"%@",self->_firstName); } @end
經過程序得出結論以下: 測試
1. #import引入頭文件,該命令能夠保證頭文件只被包含一次,不管此命令在文件中出現多少次,c語言使用#include命令。ui
2. 每一個方法前面都有一個"+" 或者 "-"符號,+表示該方法爲類方法由類調用,-表示該方法爲實例方法由實例調用。this
3. self關鍵字表引用實例對象自身,與.NET中的this關鍵字功能類似。atom
3. NSObject是Objective-c中基類,自定義類建議繼承NSObject,Objective-C不支持多繼承,可是能夠經過category(類別) 和 protocol(協議)實現多繼承的效果 spa
4. description方法繼承與NSObject,在Person中被重寫,在Objective-c中若是直接使用實例對象,默認調用description方法,至關於.NET中的ToString。線程
5. 在Objective-c中方法調用在一對方括號之間,形式如:[ instance method : parameter parameter ......]。
6. super用於調用超類中的實現方式,被用於做爲調用目標。
7. 類的定義使用關鍵字@interface,實現使用關鍵詞@implementation,
8. 屬性的聲明使用關鍵字@property,格式如:@property (attribute1 attribute2) type propertyName;
9. @private、@public、@protect用戶聲明私有、公有、受保護的成員變量,
測試Person類:
Person *person = [[Person alloc]initWithFirstName:@"first" lastName:@"last" age:25]; NSLog(@"%@",person); //firstName: first,lastName:last,age:25 [person printFullName]; //irst last [Person breath]; //people breathing
objective-c中得屬性可使用以下關鍵字進行修飾:
線程安全性:nonatomic, atomic[默認]
屬性讀寫性:readonly, readwrite[默認]
其餘特性:assign vs retain[strong] vs weak vs unsafe_unretained vs copy
assign[默認],主要用於非指針類型的屬性成員,基本數據類型
@protocol PersonDelegate <NSObject> //必須實現的方法 @required -(void)sleep; -(void)eat; //可實現,也可不實現的方法 @optional -(void)playBasketBall; @end
如今就讓Person繼承PersonDelegate,
Person.h:
#import <Foundation/Foundation.h> @protocol PersonDelegate <NSObject> //必須實現的方法 @required -(void)sleep; -(void)eat; //可實現,也可不實現的方法 @optional -(void)playBasketBall; @end @interface Person : NSObject<PersonDelegate> { //實例成員變量聲明 @private NSString *_firstName; @protected NSString *_lastName; @public NSInteger *_age; } //屬性聲明 @property (nonatomic,strong) NSString *firstName; @property (nonatomic,strong) NSString *lastName; @property (nonatomic,assign) NSInteger *age; //實例方法 -(id)initWithFirstName:(NSString *)firstName lastName:(NSString *)lastName age:(NSInteger *)age; -(void)printFullName; //類方法 +(void)breath; //PersonDelegate 方法 -(void)sleep; -(void)playBasketBall;
//未實現eat方法
@end
Person.m:
#import "Person.h" @implementation Person -(id)initWithFirstName:(NSString *)firstName lastName:(NSString *)lastName age:(NSInteger *)age { if(self = [super init]) { self.firstName = firstName; self.lastName = lastName; self.age = age; } return self; } -(NSString *)description { return [[NSString alloc]initWithFormat:@" firstName: %@,lastName:%@,age:%zd",self.firstName,self.lastName,self.age]; } +(void)breath { NSLog(@"people breathing"); } -(void)printFullName { NSLog(@"%@ %@",self->_firstName,self->_lastName); } //PersonDelegate 方法 -(void)sleep { NSLog(@"Person sleeping..."); } -(void)playBasketBall { NSLog(@"Person Play basketball"); } @end
測試代碼:
Person *person = [[Person alloc]initWithFirstName:@"first" lastName:@"last" age:25]; [person sleep]; //Person sleeping... [person playBasketBall]; //Person play basketball
經過測試總結以下幾點:
1. 一個協議能夠實現另外一個協議,如要實現多個協議,採用逗號隔開:<protocol1,protocol2....>。
2. @require定義的方法表示必需要求實現,@optional定義的方法表示能夠不實現,可是通過測試發現,協議中得方法在類沒有任何強制要求實現任何方法。
以上就是開始學習Objective-C的初步理解,若有不正確的地方,請指出。