首先申明下,本文為筆者學習《objective-c 基礎教程》的筆記,並加入筆者自己的理解和歸納總結。
兩種方法建立物件,這兩種方法是等價的。
init
方法中,呼叫super init
方法,並更新self
。如果self
為nil
,則初始化失敗。最後返回self
。
輸出@inte***ce shape : nsobject
@end
@implementation shape
-(id)init
return
self;}
-(nsstring *
)description
@end
intmain
(int argc,
const
char
* ar**)
return0;
}
(20, 20)
shape
有兩個屬性,使用initwithwidth: height
方法,在初始化時定義width
和height
屬性。init
方法也可以呼叫該方法初始化。
輸出@inte***ce shape : nsobject
@end
@implementation shape
-(id)init
return
self;}
-(id)initwithwidth:
(int
)w height:
(int
)h return
self;}
-(nsstring *
)description
@end
intmain
(int argc,
const
char
* ar**)
return0;
}
(20, 20)
(100, 100)
rectangle
類繼承shape
,但四個邊角可以是圓弧型的,radius
代表四個角的弧長。
輸出@inte***ce rectangle : shape -(
void
)setradius:
(int
)radius;
@end
@implementation rectangle
-(id)initwithwidth:
(int
)w height:
(int
)h return
self;}
-(void
)setradius:
(int
)r -
(nsstring *
)description
@end
intmain
(int argc,
const
char
* ar**)
return0;
}
(20, 20) radius = 10
(100, 100) radius = 20
objective-c 類
objective-c 資料類簡介
objective-c 記憶體管理
objective-c 物件初始化
objective-c 屬性值
objective-c 類別
objective-c 協議
objective-c 異常、選擇器、**塊
objective-c 鍵值編碼
objective-c 謂詞
Objective C 日記 物件初始化
oc中建立物件的方法有兩種 類名 new 類名 alloc int 分配和初始化是兩個分離的操作 來做nsobject的類方法alloc為物件分配一塊記憶體區域並將其清零,例項方法init用於獲得乙個物件並使其執行 分配 allocaton l u kei n n.分配,配置 安置 從作業系統獲得一...
objective c 分配和初始化物件
和c 類似oc中也有乙個根型別,也就是nsobject,它包含乙個alloc方法和乙個init方法。顧名思義,alloc用於申請記憶體,建立乙個物件的內部結構,並且將例項變數賦值為0。init方法賦值將變數的值賦值為預設值 似乎和0不一樣 然後執行一些其它的任務。alloc就像進入了一輛車裡,而in...
Objective C 繼承,初始化方法
一.繼承 1.繼承的上層 父類,繼承的下層 子類 2.繼承是單向的 3.繼承具有傳遞性 子類繼承父類的特徵和行為 4.子類擴充套件父類,更加具體 oc中的繼承 1.oc中的繼承,即乙個類繼承另乙個類 2.被繼承的類稱為父類或超類 3.繼承的類為子類 繼承的特點 1.oc中只允許單繼承 2.沒有父類的...