目前移動開發(fā)熱火朝天,今天在家學(xué)習(xí)最近入手的《Objective-c基礎(chǔ)教程》,初步感覺Objective-c與.Net差別很大,為了更好的學(xué)習(xí)理解Objective-c,將以筆記的形式記錄學(xué)習(xí)的知識點,方便查閱。
在Objective-c中類分為兩部分:聲明和實現(xiàn),下面定義一個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
通過程序得出結(jié)論如下:
1. #import引入頭文件,該命令可以保證頭文件只被包含一次,無論此命令在文件中出現(xiàn)多少次,c語言使用#include命令。
2. 每個方法前面都有一個"+" 或者 "-"符號,+表示該方法為類方法由類調(diào)用,-表示該方法為實例方法由實例調(diào)用。
3. self關(guān)鍵字表引用實例對象自身,與.NET中的this關(guān)鍵字功能相似。
3. NSObject是Objective-c中基類,自定義類建議繼承NSObject,Objective-C不支持多繼承,但是可以通過category(類別) 和 protocol(協(xié)議)實現(xiàn)多繼承的效果
4. description方法繼承與NSObject,在Person中被重寫,在Objective-c中如果直接使用實例對象,默認調(diào)用description方法,相當于.NET中的ToString。
5. 在Objective-c中方法調(diào)用在一對方括號之間,形式如:[ instance method : parameter parameter ......]。
6. super用于調(diào)用超類中的實現(xiàn)方式,被用于作為調(diào)用目標。
7. 類的定義使用關(guān)鍵字@interface,實現(xiàn)使用關(guān)鍵詞@implementation,
8. 屬性的聲明使用關(guān)鍵字@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中得屬性可以使用如下關(guān)鍵字進行修飾:
線程安全性:nonatomic, atomic[默認]
屬性讀寫性:readonly, readwrite[默認]
其他特性:assign vs retain[strong] vs weak vs unsafe_unretained vs copy
assign[默認],主要用于非指針類型的屬性成員,基本數(shù)據(jù)類型
@protocol PersonDelegate <NSObject>//必須實現(xiàn)的方法@required-(void)sleep;-(void)eat;//可實現(xiàn),也可不實現(xiàn)的方法@optional-(void)playBasketBall;@end
現(xiàn)在就讓Person繼承PersonDelegate,
Person.h:
#import <Foundation/Foundation.h>@protocol PersonDelegate <NSObject>//必須實現(xiàn)的方法@required-(void)sleep;-(void)eat;//可實現(xiàn),也可不實現(xiàn)的方法@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;
//未實現(xiàn)eat方法
@endPerson.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
通過測試總結(jié)如下幾點:
1. 一個協(xié)議可以實現(xiàn)另一個協(xié)議,如要實現(xiàn)多個協(xié)議,采用逗號隔開:<protocol1,protocol2....>。
2. @require定義的方法表示必須要求實現(xiàn),@optional定義的方法表示可以不實現(xiàn),但是經(jīng)過測試發(fā)現(xiàn),協(xié)議中得方法在類沒有任何強制要求實現(xiàn)任何方法。
以上就是開始學(xué)習(xí)Objective-C的初步理解,如有不正確的地方,請指出。
新聞熱點
疑難解答