1.什么是復(fù)合?在Objective-C 中司浪,復(fù)合是通過(guò)包含作為實(shí)例變量的對(duì)象指針實(shí)現(xiàn)的:(我們定義一輛車有一個(gè)發(fā)動(dòng)機(jī)和四個(gè)輪微饥,程序如下)[plain] view plaincopy #import@interface? Tire : NSObject
@end//Tire接口
@implementation? Tire
- (NSString *) description
{
return(@"I? am? a? tire, I? last? a? while");
}//description的實(shí)現(xiàn)
@end//Tire的實(shí)現(xiàn)
@interface? Engine? : NSObject
@end//Engine接口
@implementation? Engine
- (NSString *) description
{
return(@"I am an engine. Vroom!");
}
@end//Engine的實(shí)現(xiàn)
@interface Car : NSObject
{
Engine? *engine;
Tire? *tires[ 4 ];
}
- (void) print;
@end//Car接口
@Implementation? Car
- (id) init
{
if (self? = [super? init] ){
engine? =? [ Engine? new ];
tires[ 0 ] = [ Tire new ];
tires[ 1 ] = [ Tire new ];
tires[ 2 ] = [ Tire new ];
tires[ 3 ] = [ Tire new ];
}
return(self);
}//init方法實(shí)現(xiàn)
-(void) print
{
NSLog (@"%@" , engine);
NSLog (@"%@" , tires[0]);
NSLog (@"%@" , tires[1]);
NSLog (@"%@" , tires[2]);
NSLog (@"%@" , tires[3]);
}//print方法的實(shí)現(xiàn)
@end//Car的實(shí)現(xiàn)
int? main (int? argc, const? char * argv[ ])
{
Car? *car;
car? = [ Car? new];
[ car print ];
return (0);
}//main :運(yùn)行結(jié)果為遂庄,I am an engine, Vrooml
I am a tire , I last a? while.
I am a tire , I last a? while.
I am a tire , I last a? while.
I am a tire , I last a? while.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2.下面我來(lái)詳解看看這個(gè)程序要注意的一些地方:
(1)自定義NSLog( ):從上面程序我們可以看到輪和發(fā)動(dòng)機(jī)都有一個(gè)description方法,程序運(yùn)行時(shí)是這樣的:NSlog( )給當(dāng)前對(duì)象發(fā)送描述消息(description),r
然后對(duì)象的description方法生產(chǎn)一個(gè)NSString并返回塔拳,之后NSLog( )在其輸出中包含這個(gè)字符串鼠证。在類中添加description方法就可以自定義NSLog( )如何輸出對(duì)象
(2)engine和tires實(shí)例變量是復(fù)合的,因?yàn)樗鼈兪荂ar的實(shí)例變量靠抑。每個(gè)Car對(duì)象都會(huì)為指向engine和tires的指針?lè)峙鋬?nèi)存量九,但是真正包含在Car中的并不是engine
和tires變量,而只是內(nèi)存中存在的其他對(duì)象的引用颂碧。這些指針開始會(huì)被初始化為nil(零值)
(3)來(lái)看看Car類做了些什么荠列,第一步,為對(duì)象分配內(nèi)存载城,即對(duì)象獲得一個(gè)用來(lái)存放其實(shí)例變量的內(nèi)存塊肌似,第二步,自動(dòng)調(diào)用init方法诉瓦,讓對(duì)象處于可以狀態(tài)川队。
(4) if (self? = [super? init] )這句的具體含義在后面會(huì)說(shuō)明力细。
3.存取方法:
(1)什么是存取方法?它是來(lái)讀取或改變對(duì)象特定屬性的方法固额。一般分為眠蚂,setter方法和getter方法《匝看看一下例子:
[plain] view plaincopy
@interface? Car : NSObject
{
Engine? *engine;
Tire? *tires[ 4 ];
}
-(Engine *) engine;
-(void *) setEngine : (Engine *)newEngine;
-(Tire *) tireAtindex : ( int ) index;
-(void) setTire: (Tire *)tire
atIndex: (int) index;
-(void) print;
@ens//Car
@implementation? Car
-(Engine *) engine
{
return (engine);
}//engine
-(void )setEngine : (Engine *) newEngine
{
engine? =? newEngine;
}//setEngine
-(void) setTire? : (Tire *) tire
atindex? : (int) index
{
if(index < 0 || index > 3){
NSLog (@"bad index (%d) in setTire : atIndex : ", index);
exit (1);
}
tires[ index ] = tire;
}//seTire : atIndex :
-(Tire? *) tireAtIndex : (int) index
{
if( index < 0 || index >3){
NSLog (@"bad? index? (%d)? in "tireAtIndex :" , index );
exit (1);
}
return (tires [ index ] );
}//tireAtIndex :
@end//Car的實(shí)現(xiàn)
//////////////////////////////////////////////////////////////////////////////////////////////////////////
想實(shí)際使用這些存取方法河狐,可以像下面這樣寫:Engine? *engine = [ Engine? new ] ;
[ car setEngine : engine ];
NSLog (@"the car Engine is %@" , [car engine]);
Tire *tire = [? Tire new ];
[car setTire : tire
atIndex : 2 ];
NSLog (@"tire number two is %@",[ car tireAtIndex : 2 ]);
4.以上程序需要注意的一些地方:
(1)setter方法根據(jù)它所更改的屬性的名稱來(lái)命名米绕,并加上前綴”set“,而getter方法則僅僅根據(jù)其返回的屬性名稱來(lái)命名前面不加”get“
(2)在以上程序中可以看到:-(void) setTire? : (Tire *) tire? atindex : (int) index 瑟捣,設(shè)置輪的方法,這方法這樣寫是什么意思呢?由于汽車的4個(gè)輪胎都有自己不同的位置栅干,所以為汽車配置某個(gè)輪胎的時(shí)候迈套,不僅要知道使用哪個(gè)輪胎,還要清楚每個(gè)輪在 汽車上的位置碱鳞,同樣桑李,當(dāng)訪問(wèn)汽車上某個(gè)輪胎時(shí),也要指定這個(gè)輪的具體位置才行
5.添加了設(shè)置屬性和獲取屬性等存儲(chǔ)方法后對(duì)應(yīng)的main()函數(shù):
[plain] view plaincopy
int? main (int? argc,? const? char * argv[ ])
{
Car *car? =? [Car? new];
Engine *engine = [Engine? new];
[ car setEngine : engine ];
int? i;
for (i = 0 ; i <4 ; i++){
Tire *tire = [ Tire new];
[car? setTire : tire
atIndex? : i ];
}
[ car print ];
return(0);
}//main窿给,輸出結(jié)果與之前一樣
6.另外我們還可以自定義兩個(gè)類贵白,分別繼承Engine和Tire,并且可以對(duì)它的方法復(fù)寫崩泡。具體怎么復(fù)合和繼承配合使用這里就不多說(shuō)了禁荒,比較簡(jiǎn)單~
7.判斷是復(fù)合還是繼承的方法很簡(jiǎn)單:當(dāng)某件事物里有什么這是復(fù)合,當(dāng)某事物是什么這是繼承角撞,用這個(gè)方法就能簡(jiǎn)單的把它們區(qū)分開來(lái)呛伴。
OC是增強(qiáng)了C的特性,所以在變量和基本數(shù)據(jù)類型上基本與C一致谒所。在OC中變量命名有如下規(guī)則:由字母热康、數(shù)字、下劃線劣领、$符號(hào)組成必須以字母姐军、下劃線、$符號(hào)開頭大小寫敏感在OC中定義變量的時(shí)候不能使用OC的保留字尖淘,OC的保留字如下:OC中有如下基本數(shù)據(jù)類型:int:聲明整型變量double:聲明雙精度變量float:聲明浮點(diǎn)型變量char:聲明字符型變量id:通用的指針類型enum:聲明枚舉類型long:聲明長(zhǎng)整型變量或函數(shù)short:聲明短整型變量或函數(shù)signed:聲明有符號(hào)類型變量struct:聲明結(jié)構(gòu)體變量union:聲明共用體(聯(lián)合)數(shù)據(jù)類型unsigned:聲明無(wú)符號(hào)類型變量void:聲明函數(shù)無(wú)返回值或無(wú)參數(shù)據(jù)類型格式化字符:數(shù)據(jù)類型 oc關(guān)鍵字 格式說(shuō)明引導(dǎo)符整型 int %d.%i短整型 short int %hd.%hi長(zhǎng)類型 long int %ld.%li無(wú)符號(hào)短整型 unsigned int %u無(wú)短整型 unsigned short %hu無(wú)符號(hào)長(zhǎng)整型 unsigned long %lu浮點(diǎn)型 float %f雙精度型 double %f長(zhǎng)雙精度型 long double %lf字符型 char %c下面有一個(gè)例子程序:[plain] view plain copy//? //? main.m? //? mxy01-dataType? //? //? Created by mxy on 13-9-7.? //? Copyright (c) 2013年 mxy. All rights reserved.? //? ? #importint main(int argc, const char * argv[])
{
@autoreleasepool {
//保留字是不能定義為變量名的庶弃,例如下面的int、float等就不能作為變量名
int i = 2;
float f = 2.3f;
double d = 2.3e12;
char c = 'a';
//輸出數(shù)據(jù)
NSLog(@"i : %d",i);
NSLog(@"f : %f 截取后 : %.2f",f,f);
NSLog(@"d : %e 截取后 : %.2e",d,d);
NSLog(@"c : %c , %d",c ,c );
//數(shù)據(jù)轉(zhuǎn)換
//數(shù)據(jù)類型容量大的轉(zhuǎn)成小的可能會(huì)丟失精度
int i2 = (int)f;
float f2 = (float)i ;
NSLog(@"數(shù)據(jù)轉(zhuǎn)換");
NSLog(@"i2 : %d",i2);
NSLog(@"f2 : %f",f2);
NSLog(@"變量的作用域");
if(YES){
int i3 = 2;
NSLog(@"i3 : %d",i3);
}
//在if的{}外面是無(wú)法訪問(wèn)到內(nèi)部的i3變量的德澈,因?yàn)閕3變量的作用域就只是那對(duì){}內(nèi)部
//NSLog(@"i3 : %d",i3);
/*