
- Objective-C 基礎
- Objective-C - 首頁
- Objective-C - 概述
- Objective-C - 環境搭建
- Objective-C - 程式結構
- Objective-C - 基本語法
- Objective-C - 資料型別
- Objective-C - 變數
- Objective-C - 常量
- Objective-C - 運算子
- Objective-C - 迴圈
- Objective-C - 決策
- Objective-C - 函式
- Objective-C - 塊
- Objective-C - 數字
- Objective-C - 陣列
- Objective-C - 指標
- Objective-C - 字串
- Objective-C - 結構體
- Objective-C - 預處理器
- Objective-C - Typedef
- Objective-C - 型別轉換
- Objective-C - 日誌處理
- Objective-C - 錯誤處理
- 命令列引數
- 高階 Objective-C
- Objective-C - 類與物件
- Objective-C - 繼承
- Objective-C - 多型
- Objective-C - 資料封裝
- Objective-C - 分類
- Objective-C - 偽裝
- Objective-C - 擴充套件
- Objective-C - 協議
- Objective-C - 動態繫結
- Objective-C - 複合物件
- Obj-C - Foundation 框架
- Objective-C - 快速列舉
- Obj-C - 記憶體管理
- Objective-C 有用資源
- Objective-C - 快速指南
- Objective-C - 有用資源
- Objective-C - 討論
Objective-C 類與物件
Objective-C 程式語言的主要目的是為 C 程式語言新增面向物件特性,而類是支援面向物件程式設計的 Objective-C 的核心特性,通常被稱為使用者定義型別。
類用於指定物件的形態,它將資料表示和操作該資料的方法組合到一個整潔的包中。類中的資料和方法稱為類的成員。
Objective-C 特性
類在兩個不同的部分中定義,即@interface 和@implementation。
幾乎所有東西都以物件的形式存在。
物件接收訊息,物件通常被稱為接收者。
物件包含例項變數。
物件和例項變數具有作用域。
類隱藏物件的實現。
屬性用於在其他類中提供對類例項變數的訪問。
Objective-C 類定義
定義類時,您定義了資料型別的藍圖。這實際上並沒有定義任何資料,但它確實定義了類名的含義,即類物件將包含什麼以及對這樣的物件可以執行哪些操作。
類定義以關鍵字@interface開頭,後跟介面(類)名稱;以及由一對花括號括起來類體。在 Objective-C 中,所有類都派生自稱為NSObject的基類。它是所有 Objective-C 類的超類。它提供基本方法,例如記憶體分配和初始化。例如,我們使用關鍵字class定義 Box 資料型別,如下所示:
@interface Box:NSObject { //Instance variables double length; // Length of a box double breadth; // Breadth of a box } @property(nonatomic, readwrite) double height; // Property @end
例項變數是私有的,只能在類實現內部訪問。
分配和初始化 Objective-C 物件
類為物件提供藍圖,因此基本上物件是從類建立的。我們使用與宣告基本型別變數完全相同的宣告來宣告類的物件。以下語句聲明瞭兩個 Box 類的物件:
Box box1 = [[Box alloc]init]; // Create box1 object of type Box Box box2 = [[Box alloc]init]; // Create box2 object of type Box
box1 和 box2 這兩個物件都將擁有自己的資料成員副本。
訪問資料成員
可以使用直接成員訪問運算子 (.) 訪問類物件的屬性。讓我們嘗試以下示例來說明:
#import <Foundation/Foundation.h> @interface Box:NSObject { double length; // Length of a box double breadth; // Breadth of a box double height; // Height of a box } @property(nonatomic, readwrite) double height; // Property -(double) volume; @end @implementation Box @synthesize height; -(id)init { self = [super init]; length = 1.0; breadth = 1.0; return self; } -(double) volume { return length*breadth*height; } @end int main() { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; Box *box1 = [[Box alloc]init]; // Create box1 object of type Box Box *box2 = [[Box alloc]init]; // Create box2 object of type Box double volume = 0.0; // Store the volume of a box here // box 1 specification box1.height = 5.0; // box 2 specification box2.height = 10.0; // volume of box 1 volume = [box1 volume]; NSLog(@"Volume of Box1 : %f", volume); // volume of box 2 volume = [box2 volume]; NSLog(@"Volume of Box2 : %f", volume); [pool drain]; return 0; }
編譯並執行上述程式碼時,將產生以下結果:
2013-09-22 21:25:33.314 ClassAndObjects[387:303] Volume of Box1 : 5.000000 2013-09-22 21:25:33.316 ClassAndObjects[387:303] Volume of Box2 : 10.000000
屬性
在 Objective-C 中引入屬性是為了確保可以在類外部訪問類的例項變數。
屬性的各個部分如下所示。屬性以@property開頭,這是一個關鍵字
後面跟著訪問說明符,即 nonatomic 或 atomic、readwrite 或 readonly 以及 strong、unsafe_unretained 或 weak。這根據變數的型別而有所不同。對於任何指標型別,我們可以使用 strong、unsafe_unretained 或 weak。類似地,對於其他型別,我們可以使用 readwrite 或 readonly。
後面跟著變數的資料型別。
最後,我們有一個以分號結尾的屬性名稱。
我們可以在實現類中新增 synthesize 語句。但在最新的 XCode 中,合成部分由 XCode 處理,您無需包含 synthesize 語句。
只有使用屬性,我們才能訪問類的例項變數。實際上,內部為屬性建立了 getter 和 setter 方法。
例如,假設我們有一個屬性@property (nonatomic ,readonly ) BOOL isDone。在後臺,建立瞭如下所示的 setter 和 getter。
-(void)setIsDone(BOOL)isDone; -(BOOL)isDone;