黑馬程式員-構造方法

來源:互聯網
上載者:User

標籤:style   blog   color   io   strong   div   sp   log   on   

一、構造方法的作用

用來初始化對象的成員變數值,當我們在建立對象時,需要一些成員變數具有一定的初始值,這個時候就要用到構造方法。利用類的new方法可以建立一個對象,這個new方法其實是分兩步來完成建立對象的。

1> 調用+alloc來分配儲存空間  Person *p1 = [Person alloc];

2> 調用-init進行初始化,Person *p2 = [p1 init];

這樣我們就可以重寫-init方法進行一些成員變數的初始化了。看下面執行個體

1 #import <Foundation/Foundation.h>2 3 @interface Person : NSObject4 @property int age;5 @end
 1 #import "Person.h" 2  3 @implementation Person 4  5 - (id)init 6 { 7     if ( self = [super init] ) 8     { // 初始化成功 9         _age = 10;10     }11     12     // 3.返回一個已經初始化完畢的對象13     return self;14 }15 16 @end
1 #import "Person.h"2 3 @interface Student : Person4 @property int no;5 @end
 1 #import "Student.h" 2  3 @implementation Student 4  5 // 學生對象初始化完畢後,年齡就是10,學號就是1 6  7 - (id)init 8 { 9     if ( self = [super init] )10     {11         _no = 1;12     }13     return self;14 }15 16 @end

 

需要注意的是:一定要調用super的init方法來初始化父類中聲明的一些成員變數和其他屬性,在進行子類內部成員變數的初始化。

二、自訂構造方法

有時我們需要在建立某個對象的同時給對象一個和其他對象不同的成員變數初始值,此時需要自訂構造方法。

自訂構造方法的規範
1> 一定是對象方法,一定以 - 開頭
2> 傳回值一般是id類型
3> 方法名一般以initWith開頭

看下面的執行個體:

 1 #import <Foundation/Foundation.h> 2  3 @interface Person : NSObject 4 @property NSString *name; 5 @property int age; 6  7 - (id)initWithName:(NSString *)name; 8  9 - (id)initWithAge:(int)age;10 11 - (id)initWithName:(NSString *)name andAge:(int)age;12 13 @end
 1 #import "Person.h" 2  3 @implementation Person 4  5 - (id)init 6 { 7     if ( self = [super init] ) 8     { 9         _name = @"Jack";10     }11     return self;12 }13 14 - (id)initWithName:(NSString *)name andAge:(int)age15 {16     if ( self = [super init] )17     {18         _name = name;19         _age = age;20     }21     return self;22 }23 24 @end
 1 #import "Person.h" 2  3 @interface Student : Person 4 @property int no; 5  6 - (id)initWithNo:(int)no; 7  8 - (id)initWithName:(NSString *)name andAge:(int)age andNo:(int)no; 9 10 @end
#import "Student.h"@implementation Student- (id)initWithNo:(int)no{    if ( self = [super init] )    {        _no = no;    }    return self;}// 父類的屬性交給父類方法去處理,子類方法處理子類自己的屬性- (id)initWithName:(NSString *)name andAge:(int)age andNo:(int)no{    // 將name、age傳遞到父類方法中進行初始化    if ( self = [super initWithName:name andAge:age])    {        _no = no;    }        return self;}@end

自訂子類構造方法的時候,如果需要重新初始化父類傳下來的一些成員變數,應該首先看父類中有沒有這些成員變數的建構函式,如果有的話就調用父類的建構函式。也就是說我們應該遵循這種原則:父類的屬性應該交給父類方法去處理,子類方法處理子類自己的屬性。上面student子類構造方法的實現如果如下這樣寫的話會顯得很不專業。

 1 #import "Student.h" 2  3 @implementation Student 4 - (id)initWithNo:(int)no 5 { 6     if ( self = [super init] ) 7     { 8         _no = no; 9     }10     return self;11 }12 13 - (id)initWithName:(NSString *)name andAge:(int)age andNo:(int)no14 {15      if ( self = [super init] )16      {17         _no  = no;18         self.name = name;19         self.age = age;20         //[self setName:name];21         //[self setAge:age];22    }23     24     return self;25 }26 @end

這樣寫的缺點在於

1> 父類的屬性應該交給父類方法去處理,子類方法處理子類自己的屬性

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.