鍍金池/ 教程/ C/ Objective-C 類&對(duì)象
Objective-C 多態(tài)性
Objective-C 預(yù)處理器
Objective-C for循環(huán)
Objective-C 開發(fā)環(huán)境(安裝配置)
Obj-C Foundation/基礎(chǔ)框架
Objective-C 指針運(yùn)算
Objective-C 循環(huán)
Objective-C 錯(cuò)誤處理
Objective-C while循環(huán)
Objective-C if語句
Objective-C do...while循環(huán)
Objective-C教程
Objective-C 嵌套switch語句
Objective-C 函數(shù)按值調(diào)用
Objective-C 常量
Objective-C 運(yùn)算符
Objective-C 數(shù)據(jù)類型
Objective-C 邏輯運(yùn)算符
Objective-C 數(shù)組作為函數(shù)參數(shù)傳遞
Objective-C struct/結(jié)構(gòu)
Objective-C 嵌套循環(huán)
Objective-C 函數(shù)引用調(diào)用
Objective-C 日志處理
Objective-C 數(shù)據(jù)存儲(chǔ)
Objective-C 教程首頁
Objective-C 擴(kuò)展
Objective-C 異常處理
Objective-C 類型轉(zhuǎn)換
Objective-C 嵌套 if 語句
Objective-C typedef
Objective-C 決策
Objective-C 指針的數(shù)組
Objective-C Protocols/協(xié)議
Objective-C 日期和時(shí)間
Objective-C 指針
Objective-C 內(nèi)存管理
命令行參數(shù)
Objective-C NSString/字符串
Objective-C 動(dòng)態(tài)綁定
Objective-C 復(fù)合對(duì)象
Objective-C Arrays/數(shù)組
Objective-C 位運(yùn)算符
Objective-C 指向指針的指針
Objective-C 從函數(shù)返回?cái)?shù)組
Objective-C Posing/冒充
Objective-C Categories/類別
Objective-C 文件處理
Objective-C 賦值運(yùn)算符
Objective-C 函數(shù)返回指針
Objective-C if...else 語句
Objective-C switch語句
Objective-C URL加載系統(tǒng)
Objective-C 算術(shù)運(yùn)算符
Objective-C Numbers/數(shù)字
Objective-C語言程序結(jié)構(gòu)
Objective-C 快速枚舉
Objective-C 基本語法
Objective-C 類&對(duì)象
Objective-C 變量
Objective-C 關(guān)系運(yùn)算符
Objective-C 塊
Objective-C break語句
Objective-C continue語句
Objective-C 語言概述
Objective C 文本和字符串
Objective-C 函數(shù)
Objective-C 傳遞函數(shù)的指針
Objective-C 數(shù)組的指針
Objective-C 多維數(shù)組
Objective-C 繼承
Objective-C 數(shù)據(jù)封裝

Objective-C 類&對(duì)象

Objective-C編程語言的主要目的是增加面向?qū)ο蟮?C++編程語言,類是核心支持面向?qū)ο缶幊碳?Objective-C 的特點(diǎn),通常被稱為用戶定義的類型。

類是用來指定對(duì)象的形式,它結(jié)合了數(shù)據(jù)表示和方法操縱這些數(shù)據(jù)轉(zhuǎn)換成一個(gè)整齊的包。在一個(gè)類的數(shù)據(jù)和方法,被稱為類的成員。

Objective-C的特點(diǎn)

  • 類定義在兩個(gè)不同的部分,即 @interface 和 @implementation.

  • 幾乎所有東西都以對(duì)象的形式。

  • 對(duì)象接收消息和對(duì)象通常被稱為接收器。

  • 對(duì)象包含實(shí)例變量。

  • 對(duì)象和實(shí)例變量的范圍。

  • 類隱藏對(duì)象的實(shí)現(xiàn)。

  • 屬性是用來提供訪問其他類的類的實(shí)例變量。

Objective-C的類定義:

當(dāng)定義一個(gè)類,定義的數(shù)據(jù)類型的結(jié)構(gòu)。 這實(shí)際上并不定義任何數(shù)據(jù),但它定義的類的名字的意思是什么,即是什么類的對(duì)象將包括這樣一個(gè)對(duì)象上執(zhí)行什么操作可以。

類定義開始用關(guān)鍵字 @interface 接口(類)的名稱和類主體,由一對(duì)花括號(hào)括起來。 Objective-C中所有的類都派生自基類NSObject。它是所有的Objective-C類的超類。它提供了基本的方法,如內(nèi)存分配和初始化。例如,我們定義框數(shù)據(jù)類型使用關(guān)鍵字 class 如下:

@interface Box:NSObject
{
    //Instance variables
    double length;   // Length of a box
    double breadth;  // Breadth of a box
}
@property(nonatomic, readwrite) double height; // Property

@end

實(shí)例變量是私有的,只能訪問內(nèi)部類實(shí)現(xiàn)。

Objective-C 對(duì)象分配和初始化:

一個(gè)類提供對(duì)象的圖紙,所以基本上是一個(gè)從一個(gè)類對(duì)象被創(chuàng)建。我們聲明一個(gè)類的對(duì)象的排序完全相同的聲明,我們基本類型的變量聲明。下面的語句聲明了兩個(gè)對(duì)象,Box類:

Box box1 = [[Box alloc]init];     // Create box1 object of type Box
Box box2 = [[Box alloc]init];     // Create box2 object of type Box

兩個(gè)對(duì)象box1和box2 都會(huì)有自己的數(shù)據(jù)成員的副本。

訪問的數(shù)據(jù)成員:

一個(gè)類的對(duì)象的屬性可以直接使用成員訪問運(yùn)算符(.)訪問。讓我們來嘗試下面的例子:

#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;
}

讓我們編譯和運(yùn)行上面的程序,這將產(chǎn)生以下結(jié)果:

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中引入的屬性,以確保類的實(shí)例變量可以在類的外部訪問。

各部分屬性聲明如下。
  • 屬性由@property開始,這是一個(gè)關(guān)鍵字
  • 其次是訪問指示符,非原子或原子,讀寫或只讀強(qiáng),unsafe_unretained 或弱。變化根據(jù)不同的變量。對(duì)于任何類型的指針,我們可以使用強(qiáng)大或unsafe_unretained。同樣,對(duì)于其他類型,我們可以使用讀寫或只讀。
  • 其次是數(shù)據(jù)類型的變量。
  • 最后,我們必須以分號(hào)結(jié)束的屬性名稱。
  • 我們可以在實(shí)現(xiàn)類中添加合成語句。但在最新的Xcode,合成部分XCode在生成,需要不包括合成語句。

這是唯一可能的屬性,我們可以訪問類的實(shí)例變量。其實(shí)內(nèi)部的屬性創(chuàng)建getter和setter方法??。

例如,讓我們假設(shè)我們有一個(gè)屬性@property (nonatomic ,readonly ) BOOL isDone。有如下圖所示,創(chuàng)建 getter 和 setter 方法??。

-(void)setIsDone(BOOL)isDone;
-(BOOL)isDone;