[學習筆記—Objective-C]《Objective-C-基礎教程 第2版》第十章 對象初始化

來源:互聯網
上載者:User

標籤:objective-c   ios   ios開發   對象初始化   

10.1 指派至

向某個類發送alloc訊息,能為類分配一塊足夠大的記憶體用來存放該類的全部執行個體變數,同時alloc還將這塊記憶體地區全部初始化為0.

剛分配的對象並不能立即使用,需要先初始化。

初始化:從作業系統取得一塊記憶體儲存物件。
init方法返回其正在初始化的對象

10.11 初始化對象

注意:初始化方法返回的對象跟分配的對象可能不同。例:字串初始化函數可能會決定建立一個不同類的對象。

10.12 編寫初始化方法
//讓超類完成其自身的初始化工作self = [super init]//更新self,以便其後的執行個體變數的引用可以被映射到正確的記憶體位置
if(self = [super init])//如果init返回nil,表明未能初始化該對象

注意:將賦值和條件判斷是一種常見的技術

10.13 初始化時要做些什麼

兩種不同的初始化方式:
1. init方法建立了該對象包含的其它對象
2. init方法只建立該對象本身

惰性求值(lazy evaluation):即使你目前沒有設定自訂屬性的值,也應該等到調用者需要時再建立對象。

10.2 便利初始化函數

便利初始化函數(convenience initializer):用來完成某些額外工作的初始化方法,可減輕負擔。

如果對象必須要用某些資訊進行初始化,那麼應該將這些資訊座位init方法的一部分添加進來。

10.3 更多組件改進

將輪胎的數組形式改為NSMutableArray形式

@interface Car : NSObject{   NSMutableArray *tires;}-(void)setTire: (Tire *) tire atIndex: (int) index;-(Tire *) tireAtIndex: (int) index;

init 方法:

- (id) init {  if (self = [super init])   {     tires = [[NSMutableArray alloc] init];     int i;     for (i = 0; i < 4; i++)     {      [tires addObject: [NSNull null]]; //添加4個NSNull對象     }     //初始化即產生4個元素的數組}  return (self);} // init

存取方法:

- (void) setTire: (Tire *) tire atIndex: (int) index{  [tires replaceObjectAtIndex: index withObject: tire];  //以每個輪胎為單位  // 數組被銷毀時,將釋放數組中所有對象,tire也會被清理} 
- (Tire *) tireAtIndex: (int) index{    Tire *tire;    tire = [tires objectAtIndex: index];    return (tire);    //可簡化為:return([tires objectiveAtIndex: index]);} 
10.4 Car類的記憶體清理

原來建立tire對象方式:

        Tire *tire;        tire = [[Tire alloc] init];        [tire setPressure: 23 + i];        [tire setTreadDepth: 33 - i];

重新定義init方法:

@interface Tire : NSObject      {        float pressure;        float treadDepth;      }      - (id) initWithPressure: (float) pressure treadDepth: (float) treadDepth;      - (void) setPressure: (float) pressure;      - (float) pressure;      - (void) setTreadDepth: (float) treadDepth;      - (float) treadDepth;@end 

新init方法實現:

- (id) initWithPressure: (float) p treadDepth: (float) td      {        if (self = [super init])         {                    pressure = p;          treadDepth = td;        }        return (self);      } 

建立tire對象新方式:

      Tire *tire;      tire = [[Tire alloc] initWithPressure: 23 + i treadDepth: 33 - i];
10.5 指定初始化函數

tire類的四個初始化方法:

- (id) init {  if (self = [super init]) {    pressure = 34.0;    treadDepth = 20.0;}  return (self);} // init- (id) initWithPressure: (float) p{  if (self = [super init]) {    pressure = p;    treadDepth = 20.0;}  return (self);} // initWithPressure- (id) initWithTreadDepth: (float) td{  if (self = [super init]) {    pressure = 34.0;    treadDepth = td;```}  return (self);} // initWithTreadDepth- (id) initWithPressure: (float) p             treadDepth: (float) td{        if (self = [super init]) {          pressure = p;          treadDepth = td;        }        return (self);} // initWithPressure:treadDepth:

Tire類新初始化方式:

- (id) init{    if (self = [self initWithPressure: 34                     treadDepth: 20]) {    }    return (self);} // init- (id) initWithPressure: (float) p{    if (self = [self initWithPressure: p                     treadDepth: 20.0]) {    }    return (self);} // initWithPressure- (id) initWithTreadDepth: (float) td{    if (self = [self initWithPressure: 34.0                           treadDepth: td])     {    }    return (self);} // initWithTreadDepth- (id) initWithPressure: (float) p             treadDepth: (float) td{    if (self = [super init])     {        pressure   = p;        treadDepth = td;    }    return (self);} // initWithPressure:treadDepth:

Tire的子類的聲明檔案:

- (id) initWithPressure: (float) p treadDepth: (float) td{   //根據父類的指定初始化函數編寫    if (self = [super initWithPressure: p                            treadDepth: td]) {        rainHandling = 23.7;        snowHandling = 42.5;    }    return (self);} // initWithPressure:treadDepth

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

[學習筆記—Objective-C]《Objective-C-基礎教程 第2版》第十章 對象初始化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.