【IOS 開發】Object,ios開發object

來源:互聯網
上載者:User

【IOS 開發】Object,ios開發object

.





一. 類定義


類定義需要實現兩部分 : 

-- 介面部分 : 定義類的成員變數和方法, 方法是抽象的, 在標頭檔中定義;

-- 實現部分 : 引入介面部分的標頭檔, 實現抽象方法;



1. 介面部分定義 



(1) 介面部分定義格式


介面部分定義格式 : 

@interface className : superClassName

{

type _variableName;

... ...

}

- (type) methodName : type parameterName;

@end

-- 介面定義開始標識 : @interface 表示介面開始定義;

-- 介面定義結束標識 : @end 表示介面定義結束;

-- 介面名稱定義 : 緊跟在 @interface 後 是類名稱;

-- 父類介面 : 如果有父類, 在介面名稱後面的 : 後寫出;

-- 成員變數定義 : 在 @interface 後的 {} 中定義 成員變數;

-- 成員方法 : 在 {} 中定義完成員變數後定義成員方法;



(2) 成員變數定義

.

注意 : 成員方法中的 傳回值類型 和 參數類型必須使用 () 括起來, 以便與 C 語言方法區分;

.

成員變數簡介 : 

-- 定義格式 : 類型 成員變數名, 如 int _age;

-- 定義位置 : 在 @interface 的下一行的 大括弧中定義, 至右成員變數是定義在大括弧中的;

-- 可用類型 : 基本類型, 構造類型, 各種指標型類型;

-- 命名規範 : 成員變數一般建議 使用 "_" 開頭, 駝峰式命名;



(3) 成員方法


成員方法簡介 : 

-- 成員方法格式 : 方法類型標識 (傳回值類型) 方法名稱 : (參數類型) 參數名稱 方法簽名關鍵字 : (參數類型) 參數名稱;

-- 方法標識符 : "+" 表示類方法, "-" 表示執行個體方法;

-- 方法簽名關鍵字 : 由 方法名, 形參標籤, 冒號組成;

-- 形參標籤 : Object-C 慣例, 除了第一個形參之外, 為每個形參都制定一個形參標籤;


方法簽名意義 : 

-- "method" : 表示方法沒有參數;

-- "method : " : 表示方法有一個形參;

-- "method : param : " : 表示方法有兩個形參;



(4) 介面部分程式碼範例


樣本 : 

/*************************************************************************    > File Name: Student.h    > Author: octopus    > Mail: octopus_truth.163.com     > Created Time: 日 12/14 19:51:16 2014類介面部分樣本 ************************************************************************/#import <Foundation/Foundation.h>@interface Student : NSObject{/* * 成員變數定義在 介面聲明的花括弧中 * 注意成員變數之前以 "_" 開頭 */NSString * _name;int _age;}/* * 定義 setData : setAge : 方法 * - 表示該方法需要通過對象調用 * setAge 是形參標籤 */- (void) setData : (NSString*)name setAge:(int)age;// 定義 speak : 方法- (void) speak : (NSString*) content;// 定義一個不帶參數的方法- (NSString*) toString;+ (void) fuck;



2. 實現部分定義



(1) 實現部分定義格式


實現部分格式 : 

@implementation className

{

type _variable;

}

- (type) methodName : (type)param

{

//方法體

}

@end



(2) 類名 與 父類


實現部分類名要求 : 類名 與 類 介面部分類名相同;


父類繼承 : 如果有父類, 可以使用 "@implementation 類名 : 父類名" 表示;



(3) 實現部分的成員變數 和 成員方法


聲明成員變數 : 如果在實現部分聲明成員變數, 那麼該成員變數在外部不可訪問, 只能在實現部分訪問;


成員方法簡介 : 

-- 實現抽象成員方法 : 實現介面部分定義的抽象方法, 即為介面部分方法寫方法體;

-- 聲明成員方法 : 在實現部分聲明成員方法, 外部不可訪問, 只能在實現部分進行訪問;



(4) 抽象方法樣本


程式碼範例 : 

/*************************************************************************    > File Name: Student.m    > Author: octopus    > Mail: octopus_truth.163.com     > Created Time: 日 12/14 20:04:28 2014 ************************************************************************//* * 這裡匯入 Student.h 包即可, 因為 在 Student.h 包已經匯入了 Foundation.h 標頭檔了 */ #import <Student.h> @implementation Student {//定義的該成員變數 只能在實現部分使用, 是隱藏的成員變數;int _class; } //定義了 setData : setAge : 方法 - (void) setData : (NSString*)name setAge:(int)age {_name = name;_age = age; } - (void) speak : (NSString)content {NSLog(@"%@", content); } //定義了一個帶傳回值的方法 - (NSString*) toString {return [NSString stringWithFormat : @"name = %@, age = %d", _name, _age]; } + (void) fuck {NSLog(@"fuck"); }@end




二. 對象詳解



1. 對象 定義, 建立 和 方法調用



(1) 對象定義


變數定義文法 : 類型* 對象名;

-- 樣本 : 如上面定義的 Student 類型的對象, Student* student;



(2) 建立對象


建立對象文法 : [[類名 alloc] 初始化方法] , 樣本 [[Student alloc] init];

-- alloc : alloc 是一個關鍵字, 表示這是建立對象的語句, 相當於 Java 中得 new, 負責分配記憶體空間 和 建立對象;

-- 初始化方法 : 所有的對象都從 NSObject 繼承了一個預設的初始化方法 init;


另一種建立對象方法 : [類名 new], 樣本 [Student new] ;

-- 解析 : 這是為了照顧 Java 程式員習慣, 一般不常用;



(3) 方法調用


方法調用文法 : [調用者 方法名:參數1 形參標籤:參數2 ...], 樣本 [stident fuck];

-- 調用者 : 可以是對象 也可以是 類, 對象調用執行個體方法, 類 調用類方法;



(4) 樣本 


程式碼範例 : 

-- 代碼 : 

/*************************************************************************    > File Name: StudentTest.m    > Author: octopus    > Mail: octopus_truth.163.com     > Created Time: 日 12/28 21:58:41 2014 ************************************************************************/#import <Foundation/Foundation.h>#import "Student.h"int main(int argc, char * argv[]){@autoreleasepool {//定義變數Student* student;//建立變數student = [[Student alloc] init];//方法調用, 調用類方法, 注意 類方法只能由類調用, 不能由對象調用[Student fuck];//調用帶參數的方法[student speak : @"fuck you!"];//為成員變數賦值[student setData : @"悟空" setAge : 25];//列印成員變數NSString* stuStr = [student toString];NSLog(@"Student is : %@", stuStr);}}

-- 執行結果 : 

octopus-2:oc_object octopus$ clang -fobjc-arc -framework Foundation Student.m StudentTest.moctopus-2:oc_object octopus$ ./a.out 2014-12-28 23:22:37.702 a.out[415:507] fuck2014-12-28 23:22:37.704 a.out[415:507] fuck you!2014-12-28 23:22:37.705 a.out[415:507] Student is : name = 悟空, age = 25




2. 對象 和 變數 



(1) 對象 和 指標變數概念


概念引入 : Student* student = [[Student alloc] init], 產生了 Student 對象 和 student 變數;

-- 對象 : 建立了一個 Student 類型的對象;

-- 指標變數 : person 就是指向對象的指標變數;



(2) 對象 和 指標變數儲存內容


對象 和 變數的儲存內容 : 

-- 對象 : 儲存了建立的 Student 對象的 成員變數;

-- 指標變數 : 儲存了該對象在堆記憶體中得首地址, 即第一個成員變數的首地址;



(3) 對象 和 指標變數 的位置


對象 和 變數的儲存地址 : 

-- 對象 : 對象儲存在 堆(heap)記憶體中;

-- 指標變數 : 儲存在main()方法的動態儲存裝置區中;



(4) 對象 與 指標變數對應關係


對應關係 : 一個指標只能指向一個對象, 一個對象可以有多個指標指向她;



(5) 對象釋放


記憶體泄露 : 如果沒有指標指向某個對象, 那麼這個對象需要被釋放掉, 否則會造成記憶體泄露;

-- 解決方案 : 使用自動引用計數, 即將程式放在 @autoreleasepool{}中;




3. self 關鍵字



(1) 調用對象本身的方法 或 變數


調用本身方法 : 在對象中, 調用對象本身的方法 或 變數;

-- 調用本身方法 : [self 方法名 : 參數1 形參標籤:參數2 ...] ;

-- 調用本身成員變數 : self->成員變數名 ;


注意 : self 不能調用 類方法 和 在類方法中調用, self 使用方法與 對象相同;



(2) 作用 


區分成員變數 和 局部變數 : 如果局部變數與成員變數重名, 使用 self->成員變數名 可以表示成員變數, 預設的名稱就是局部變數;



(3) 程式碼範例 


程式碼範例 : 修改上面的 Student.m 代碼, 編譯後執行, 注意有警告, 方法中得成員變數 與 形參名稱 重複;

-- 代碼 : 

/*************************************************************************    > File Name: Student.m    > Author: octopus    > Mail: octopus_truth.163.com     > Created Time: 日 12/14 20:04:28 2014 ************************************************************************//* * 這裡匯入 Student.h 包即可, 因為 在 Student.h 包已經匯入了 Foundation.h 標頭檔了 */ #import "Student.h" @implementation Student {//定義的該成員變數 只能在實現部分使用, 是隱藏的成員變數;int _class; } //定義了 setData : setAge : 方法 - (void) setData : (NSString*)_name setAge:(int)_age {//self 關鍵字用於 區分 成員變數 和 方法的形參 self->_name = _name;self->_age = _age; } - (void) speak : (NSString*)content {NSLog(@"%@", content); } //定義了一個帶傳回值的方法 - (NSString*) toString { //self 關鍵字用於調用本對象的方法[self speak : @"正在調用 toString 方法 : "];return [NSString stringWithFormat : @"name = %@, age = %d", _name, _age]; } + (void) fuck {NSLog(@"fuck"); }@end


-- 執行結果 : 

octopus-2:oc_object octopus$ clang -fobjc-arc -framework Foundation Student.m StudentTest.mStudent.m:21:16: warning: local declaration of '_name' hides instance variable [-Wshadow-ivar]        self->_name = _name;                      ^Student.m:22:15: warning: local declaration of '_age' hides instance variable [-Wshadow-ivar]        self->_age = _age;                     ^2 warnings generated.octopus-2:oc_object octopus$ ./a.out 2014-12-29 00:49:46.728 a.out[558:507] fuck2014-12-29 00:49:46.730 a.out[558:507] fuck you!2014-12-29 00:49:46.731 a.out[558:507] 正在調用 toString 方法 : 2014-12-29 00:49:46.731 a.out[558:507] Student is : name = 悟空, age = 25



4. id類型



(1) id 類型


id類型 : 代表所有的物件類型;

-- 樣本 : id student = [[Student alloc] init];



(2) 動態綁定


動態綁定 : 跟蹤對象所屬類, 在運行時判定對象所屬類, 並在運行時確定調用方法, 不是在編譯時間;

-- 使用方式 : id 類型的變數調用方法時會執行動態綁定;



(3) 程式碼範例


程式碼範例 : 

-- 代碼 : 

/*************************************************************************    > File Name: StudentTest.m    > Author: octopus    > Mail: octopus_truth.163.com     > Created Time: 日 12/28 21:58:41 2014 ************************************************************************/#import <Foundation/Foundation.h>#import "Student.h"int main(int argc, char * argv[]){@autoreleasepool {//定義變數Student* student;//建立變數student = [[Student alloc] init];//方法調用, 調用類方法, 注意 類方法只能由類調用, 不能由對象調用[Student fuck];//調用帶參數的方法[student speak : @"fuck you!"];//為成員變數賦值[student setData : @"悟空" setAge : 25];//列印成員變數NSString* stuStr = [student toString];NSLog(@"Student is : %@", stuStr);}}


--執行結果 : 

octopus-2:oc_object octopus$ clang -fobjc-arc -framework Foundation Student.m StudentTest.mStudent.m:22:16: warning: local declaration of '_name' hides instance variable [-Wshadow-ivar]        self->_name = _name;                      ^Student.m:23:15: warning: local declaration of '_age' hides instance variable [-Wshadow-ivar]        self->_age = _age;                     ^2 warnings generated.octopus-2:oc_object octopus$ ./a.out 2014-12-29 01:09:23.548 a.out[605:507] fuck2014-12-29 01:09:23.550 a.out[605:507] fuck you!2014-12-29 01:09:23.550 a.out[605:507] 正在調用 toString 方法 : 2014-12-29 01:09:23.550 a.out[605:507] Student is : name = 悟空, age = 252014-12-29 01:09:23.551 a.out[605:507] 正在調用 toString 方法 : 2014-12-29 01:09:23.551 a.out[605:507] id is : name = 八戒, age = 23




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.