Self的使用:spa
1 self不能離開類 離開類以後沒有任何意義code
2 self會自動區分類方法和對象方法對象
3 使用self的時候只須要關注self在哪個方法中blog
若是在類方法中使用self 那麼self就表明當前類get
若是在對象方法中使用self 那麼self就表明"當前調用該方法的對象" it
聲明文件.h: io
#import <Foundation/Foundation.h> @interface Phone : NSObject { // 手機牌子 NSString* _type; } /*--------------------------getter-setter方法------------------------*/ // 屬性讀寫器 - (void) setType: (NSString *) type; - (NSString *) type; /*--------------------------對象方法----------------------------------*/ // 打電話 - (void) callWithNumber: (NSString *) number; // 發短信給 - (void) sendMessageWithNumber: (NSString *) number andContent: (NSString *) content; /*---------------------------類方法----------------------------------*/ // 警告 + (void) alert; // 關機 + (void) turnOFF; @end
實現文件.m: class
#import "Phone.h" @implementation Phone /*----------------實現屬性讀寫器-------------------*/ - (void)setType:(NSString *)type{ _type = type; } - (NSString *)type{ return _type; } /*----------------實現對象方法--------------------*/ // self == 當前調用該方法的對象 == phone // 打電話 - (void)callWithNumber:(NSString *)number{ // 使用self在對象方法中訪問成員變量 NSLog(@"用 %@ 手機給 %@ 打電話", self.type, number); } //發短信 - (void)sendMessageWithNumber:(NSString *)number andContent:(NSString *)content{ NSLog(@"用 %@ 手機給 %@ 發短信 短信內容: %@", self.type, number, content); // 使用self在對象方法中調用其餘對象方法 [self callWithNumber:number]; } /*----------------實現類方法----------------------*/ // self == 當前類 == Phone // 警告 + (void)alert{ NSLog(@"警告 手機電量不足 請及時充電"); // 使用self在類方法中調用其餘類方法 [self turnOFF]; } // 關機 + (void)turnOFF{ NSLog(@"手機即將關機"); } @end
Main.m:import
#import <Foundation/Foundation.h> #import "Phone.h"
int main(int argc, const char * argv[]) { Phone * phone = [Phone new]; phone.type = @"iPhone"; [phone sendMessageWithNumber:@"13222334455" andContent:@"對象方法發短信"]; [Phone alert]; return 0; } /* 2015-08-30 14:01:23.277 self[509:24866] 用 iPhone 手機給 13222334455 發短信 短信內容: 對象方法發短信 2015-08-30 14:01:23.278 self[509:24866] 用 iPhone 手機給 13222334455 打電話 2015-08-30 14:01:23.278 self[509:24866] 警告 手機電量不足 請及時充電 2015-08-30 14:01:23.278 self[509:24866] 手機即將關機 */
Self使用注意:變量
1 在setter-getter方法中 不能使用self和點語法來對成員屬性進行賦值和取值操做 會形成死循環
/*----------------實現屬性讀寫器-------------------*/ - (void)setType:(NSString *)type{ // _type = type; //self->_type = type; self.type = type; // 等效於 [self setType: type] 循環調用 setType: 方法 } - (NSString *)type{ // return _type; // return self->_type; return self.type; // 等效於 [self type]; 循環調用 type 方法 }
2 在對象方法中調用類方法 會報錯 由於self在對象方法中表明的是對象 而類方法須要經過類來調用
//發短信 - (void)sendMessageWithNumber:(NSString *)number andContent:(NSString *)content{ NSLog(@"用 %@ 手機給 %@ 發短信 短信內容: %@", self.type, number, content); // 使用self在對象方法中調用其餘對象方法 [self callWithNumber:number]; // 錯誤信息: No visible @interface for 'Phone' declares the selector 'alert'
[self alert]; } /*----------------實現類方法----------------------*/ // self == 當前類 == Phone // 警告 + (void)alert{ NSLog(@"警告 手機電量不足 請及時充電"); // 使用self在類方法中調用其餘類方法 [self turnOFF]; }
3 在類方法中不能調用對象方法或訪問成員變量 由於對象方法和成員屬性屬於對象
/*----------------實現對象方法--------------------*/ // self == 當前調用該方法的對象 == phone // 打電話 - (void)callWithNumber:(NSString *)number{ // 使用self在對象方法中訪問成員變量 NSLog(@"用 %@ 手機給 %@ 打電話", self.type, number); } /*----------------實現類方法----------------------*/ // self == 當前類 == Phone // 警告 + (void)alert{ NSLog(@"警告 手機電量不足 請及時充電"); // 使用self在類方法中調用其餘類方法 [self turnOFF]; // 錯誤信息: No known class method for selector 'callWithNumber:' [self callWithNumber:@"13344552211"]; }
4 不能在對象方法或者類方法中利用self調用當前self所在的方法 會形成死循環
// 打電話 - (void)callWithNumber:(NSString *)number{ // 使用self在對象方法中訪問成員變量 NSLog(@"用 %@ 手機給 %@ 打電話", self.type, number); [self callWithNumber:number]; // 死循環 } // 關機 + (void)turnOFF{ NSLog(@"手機即將關機"); [self turnOFF]; // 死循環 }
self使用場景:
> 能夠用於在對象方法之間進行相互調用
> 能夠用於在類方法之間進行相互調用
> 能夠用於區分紅員變量和局部變量同名的狀況
#import "Phone.h" @implementation Phone /*----------------實現屬性讀寫器-------------------*/ - (void)setType:(NSString *)type{ // _type = type; //self->_type = type; self.type = type; // 等效於 [self setType: type] 死循環
} - (NSString *)type{ // return _type; //return self->_type; return self.type; // 等效於 [self type]; 死循環 } /*----------------實現對象方法--------------------*/ // self == 當前調用該方法的對象 == phone // 打電話 - (void)callWithNumber:(NSString *)number{ // 使用self在對象方法中訪問成員變量 NSLog(@"用 %@ 手機給 %@ 打電話", self.type, number); [self callWithNumber:number]; // 調用當前所在方法 死循環 } //發短信 - (void)sendMessageWithNumber:(NSString *)number andContent:(NSString *)content{ NSLog(@"用 %@ 手機給 %@ 發短信 短信內容: %@", self.type, number, content); // 使用self在對象方法中調用其餘對象方法 [self callWithNumber:number]; // 對象方法中調用類方法 錯誤信息: No visible @interface for 'Phone' declares the selector 'alert' [self alert]; } /*----------------實現類方法----------------------*/ // self == 當前類 == Phone // 警告 + (void)alert{ NSLog(@"警告 手機電量不足 請及時充電"); // 使用self在類方法中調用其餘類方法 [self turnOFF]; // 類方法中調用對象方法 錯誤信息: No known class method for selector 'callWithNumber:' [self callWithNumber:@"13344552211"]; // 類方法中訪問成員變量 錯誤信息: No member named 'type' in 'struct objc_class' NSLog(@"type = %@", self.type); } // 關機 + (void)turnOFF{ NSLog(@"手機即將關機"); [self turnOFF]; // 調用當前所在方法 死循環
} @end