ObjectiveC漢化版全集教程_第1頁
ObjectiveC漢化版全集教程_第2頁
ObjectiveC漢化版全集教程_第3頁
已閱讀5頁,還剩70頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

Page Objective-CBeginner'sGuide目錄l 始吧°下載這篇教學(xué)°設(shè)定環(huán)境°前言°編譯hellol創(chuàng)建Classes°@interface°@implementation°把它們湊在一路l...°多重參數(shù)°建構(gòu)子(Constructors)°存取權(quán)限°Classlevelaccess(Exceptions)處置l繼承、多型(Inheritance,Polymorphism)和其它對(duì)象導(dǎo)向功能°id型別°繼承(Inheritance)°動(dòng)態(tài)識(shí)別(Dynamictypes)°Categories°Posing°Protocolsl內(nèi)存治理°RetainandRelease(保留與釋放)°Dealloc°AutoreleasePoollFoundationFrameworkClasses°NSArray°NSDictionaryl優(yōu)勢與缺點(diǎn)l更多信息開始吧Objective-CBeginner'sGuide 頁碼,2/43o 設(shè)定環(huán)境nLinux/FreeBSD:安裝GNUStepn為了編譯GNUstep應(yīng)用程序,必需先執(zhí)行位于/usr/GNUstep/System/Makefiles/的那個(gè)檔案。那個(gè)途徑取決于你的系統(tǒng)環(huán)境,有些是在/usr,some/usr/lib,有些是/usr/local。若是你的shell是以csh/tcsh為基礎(chǔ)的shell,那么應(yīng)該改用。建議把那個(gè)指令放在.bashrc或.cshrc中。nMacOSX:安裝XCodenWindowsNT:安裝cygwin或mingw,然后安裝GNUStepo 前言據(jù)型別、什么是

n這篇教學(xué)假設(shè)你已經(jīng)有一些大體的C語言知識(shí),包括C數(shù)存治理。若是

函式、什么是回傳值、關(guān)于指針的知識(shí)和大體的C語言內(nèi)K&RTheCProgrammingLanguage(譯注:臺(tái)灣出版書名為C程序語言第版)這是C語言的設(shè)計(jì)者所寫的。是有一些例外,

nObjective-C,是C的衍生語言,繼承了所有C語言的特性??墒撬鼈儾皇抢^承于C的語言特性本身。nC/C++你或許曾利用過Objective-C那么是nil。不同之處是你能夠傳遞訊息給nil (例[nilmessage];這是完全合法的,但是你卻不能對(duì)NULL依樣畫葫蘆。nObjective-C是「真的」有。它是包括在Foundationclasses(大體類別庫(即import;nil也是包括在那個(gè)標(biāo)頭檔內(nèi)。BOOL在Objective-C中有兩種型態(tài):YES或NO,而不是TRUE或FALSE。咱們利用了

n#importvs#include:就猶如你在helloworld范例中看到的,#import。#import 由gcc編譯器支持。我并非建議利用#include,#import大體都同意,利用這

上跟.h檔頭尾的#ifndef#define#endif相同。許多程序員們?nèi)绱瞬坏軌虮?/p>

些東西這是十分愚蠢的。不管如何,利用#import就對(duì)了。gccObjective-C免麻煩,而且萬一有一天 把它拿掉了,將會(huì)有足夠的程序員們官方的程序代

能夠堅(jiān)持保留它或是將它放回來。偷偷告知你,Apple在它碼中也利用了#import。因此萬一有一天這種事真的發(fā)生,不難預(yù)料Apple將#importgcc分支版本。n在Objective-C中,method及message這兩個(gè)字是能夠互換的。只是messages擁有專門的特性,一個(gè)message能夠動(dòng)態(tài)的轉(zhuǎn)送給另一個(gè)對(duì)象。在Objective-那個(gè)訊息,而是

C中,呼對(duì)象上的一個(gè)訊息并非必然表示對(duì)象真的會(huì)實(shí)作對(duì)象明白如何以某種方式去實(shí)作它,或是轉(zhuǎn)送給明白如何實(shí)作的對(duì)象。o 編譯helloworldnS #importSS intmain(intargc,constchar*argv[])S printf("helloworld\n");andSettings\Administrator\桌面\Objective-CBegi...2020-2-6PDFcreatedwithpdfFactoryProtrialversionObjective-C Beginner's Guide頁碼,3/43S return0;}n輸出helloworldnObjective-C#import代替#includenObjective-C.m創(chuàng)建classes@interfacen基于"ProgramminginObjective-C,"Copyright bySamsPublishing一中的范例,并通過許諾而刊載。nS #importSS @interfaceFraction:NSObjectS intnumerator;S intS }SS -(void)print;S -(void)setNumerator:(int)d;S -(void)setDenominator:(int)d;S -(int)numerator;S -(int)denominator;S @endnNSObject:NeXTStepObject的縮寫。因?yàn)樗呀?jīng)更名為OpenStep,因此這在今天已經(jīng)不是那么成心義了。n繼承(inheritance)以Class:Parent表示,就像上面的Fraction:NSObject。n夾在@interfaceClass:Parent{}中的稱為instancevariables。設(shè)的存取權(quán)限為

n沒有設(shè)定存取權(quán)限(protected,public,private)時(shí),預(yù)protected。設(shè)定權(quán)限的方式將在稍后說明。nInstancemethods跟在成員變數(shù)(即instancevariables)后。格式為:scope(returnType) methodName: (parameter1Type)parameter1Name;n scope 有class 或instance 兩種。methods以- 頭,classlevelmethods以+ 頭。nInterface@end作為終止。@implementationn基于"ProgramminginObjective-C,"Copyright bySamsPublishing一中and Settings\Administrator\ 桌 面 \Objective-C Begi...2020-2-6PDFcreatedwithpdfFactoryProtrialversionObjective-C Beginner's Guide頁碼,4/43的范例,并通過許諾而刊載。nS #import""S #importSS @implementationFractionS -(void)print{S printf("%i/%i",numerator,denominator);S }SS -(void)setNumerator:(int)nS numerator=n;S }SS -(void)setDenominator:(int)dS denominator=d;S }SS -(int)denominator{S returnS }SS -(int)numerator{S returnnumerator;S }@endnImplementation 以@implementationClassName開@end終止。中宣告時(shí)很近似。

nImplement概念好的methods的方式,跟在interfaceo 把它們湊在一路n 基于"Programming in Objective-C," 2004bySamsPublishing一中的范例,并通過許諾而刊載。nS #import<>S #import""SS intmain(intargc,constchar*argv[]){S .2020-2-6PDFcreatedwithpdfFactoryProtrialversionObjective-C Beginner's Guide頁碼,5/43S [fracprint];S printf("\n");SS .o 多重參數(shù)之下不是很直覺,不

n目前為止我還沒展現(xiàn)如何傳遞多個(gè)參數(shù)。那個(gè)語法乍看過它卻是來自一個(gè)十分受歡迎的Smalltalk版本。n基于"ProgramminginObjective-C,"Copyright 2004bySamsPublishing一中的范例,并通過許諾而刊載。nS ...S -(void) setNumerator: (int) nandDenominator:(int)d;...nS ...S -(void) setNumerator: (int) nandDenominator:(int)d{andSettings\Administrator\桌面\Objective-CBegi... 2020-2-6PDFcreatedwithpdfFactoryProtrialversionObjective-C Beginner's Guide頁碼,6/43SSnumerator=n;denominator=d;S}...nS #import<>S #import""SS intmain(intargc,constchar*argv[]){S .2020-2-6PDFcreatedwithpdfFactoryProtrialversionObjective-C Beginner's Guide頁碼,7/43略label名稱,但以:區(qū)隔參數(shù)。并非建議如此利用。o 建構(gòu)子(Constructors)n 基于"Programming in Objective-C," 2004bySamsPublishing一中的范例,并通過許諾而刊載。nS ...S -(Fraction*)initWithNumerator:(int)ndenominator:(int)d;...nS ...S -(Fraction*)initWithNumerator:(int)ndenominator:(int)d{S self=[superinit];SS if(self){S [selfsetNumerator:nandDenominator:d];S }SS returnself;S }...nS #import<>S #import""SS intmain(intargc,constchar*argv[]){S .2020-2-6PDFcreatedwithpdfFactoryProtrialversionObjective-C Beginner's頁碼,8/43S [frac2print];S printf("\n");SS printf("Fraction3is:");S [frac3print];S printf("\n");SS . 2020-2-6PDFcreatedwithpdfFactoryProtrialversionObjective-C Beginner's頁碼,9/43S @privateS intprivateVar;

GuideGuideS intprivateVar2;S @protectedS intprotectedVar;S }@endnS #import""SS @implementationAccess@endnS #import""S #importSS intmain(intargc,constchar*argv[]){S Access*a=[[Accessalloc]init];SS .2020-2-6PDFcreatedwithpdfFactoryProtrialversion Page Objective-C Beginner's Guide頁碼,10/43SS staticintSS @interfaceClassA:NSObjectS +(int)initCount;S +(void)initialize;@endnS #import""SS @implementationClassAS -(id)init{S self=[superinit];S count++;S returnself;S }SS +(int)initCount{S returncount;S }SS +(void)initializeS count=0;S }@endnS #import""S #import<>SS intmain(intargc,constchar*argv[]){S ClassA*c1=[[ClassAalloc]init];S ClassA*c2=[[ClassAalloc]init];SS .2020-2-6PDFcreatedwithpdfFactoryProtrialversion Page Objective-C Beginner's Guide頁碼,11/43}noutputS ClassAcount:2ClassAcount:3nstaticintcount=0; 這是classvariable實(shí)這種變量擺在那個(gè)地址并不睬想,比較好的解法是像 Java實(shí)作staticvariables的方式。但是,它確實(shí)能用。意這細(xì)的不同!這

n+(int)initCount;這是回傳count值的實(shí)際method里在type前面不用減號(hào)-而改用加號(hào)+。加號(hào)+表示這是一個(gè)classlevelfunction(levelfunctions被classfunctions或classmethod)n存取那個(gè)變數(shù)跟存取一樣成員變數(shù)沒有兩樣,就像ClassA 中的count++用法。n+(void)initializemethodis在Objective-C也被每一個(gè)class呼。這是初始化像咱們的countclasslevelvariables的好地址。o 異樣情形(Exceptions)n注意:異樣處置只有MacOSX 以上才支持。n基于"ProgramminginObjective-C,"Copyright bySamsPublishing一中的范例,并通過許諾而刊載。nS #import<Foundation/>SS @interface CupWarningException:NSException@endnS #importSS @implementationCupWarningException@endnS #importSS @interface CupOverflowException:NSException@endnandSettings\Administrator\桌面\Objective-CBegi... 2020-2-6PDFcreatedwithpdfFactoryProtrialversion Page Objective-C Beginner's Guide頁碼,12/43S #import""SS @implementationCupOverflowException@endnS #importSS @interfaceCup:NSObjectS intlevel;S }SS -(int)level;S -(void)setLevel:(int)l;S -(void)fill;S -(void)empty;S -(void)print;@endnS #import""S #import""S #import""S #import<Foundation/>S #importSS @implementationCupS -(id)init{S self=[superinit];SS if(self){S[selfsetLevel:0];S}SSreturnself;S }SS -(int)level{S returnlevel;S }SS -(void)setLevel:(int)lS level=l;SS if(level>100){S .2020-2-6PDFcreatedwithpdfFactoryProtrialversion Page Objective-C Beginner's Guide頁碼,13/43S reason:@"Thelevelisabove100"S userInfo:nil];S @throwe;S }elseif(level>=50){S .2020-2-6PDFcreatedwithpdfFactoryProtrialversion Page Objective-C Beginner's Guide頁碼,14/43SS[cupfill];[cupprint];S}SS.}n還有一個(gè)finally區(qū)塊,它的行為就像Java的異樣處置方式,finally區(qū)塊的內(nèi)容保證會(huì)被呼。and Settings\Administrator\ 桌 面 \Objective-C Begi...2020-2-6PDFcreatedwithpdfFactoryProtrialversion Page Objective-C Beginner's Guide頁碼,15/43數(shù)物件。在

n 里的@"CupOverflowException"是一個(gè)NSString常Objective-C中,@C語言形式的字(CstringC/C++"Stringconstant"的形式,型別為char*。繼承、多型(Inheritance,Polymorphism)以及其它對(duì)象導(dǎo)向功能o id型別nObjective-C有種叫做id的型別,它的運(yùn)作有時(shí)候像是void*,只是它卻嚴(yán)格樣,你在呼一個(gè)對(duì)象

規(guī)定只能用在對(duì)象。Objective-C與Java跟C++不一的method時(shí),并非需要明白那個(gè)對(duì)象的型別。固然method必然要存在,這稱為Objective-C的訊息傳遞。n基于"ProgramminginObjective-C,"Copyright 2004bySamsPublishing一中的范例,并通過許諾而刊載。nS#import<Foundation/>SS@interfaceFraction:NSObject{Sintnumerator;Sintdenominator;S}Sdenominator:(int)d;S-(Fraction*)initWithNumerator:(int)nS-(void)print;S-(void)setNumerator:(int)d;S-(void)setDenominator:(int)d;andDenominator:(int)d;S-(void) setNumerator:(int)nS-(int)numerator;S-(int)denominator;@endnS #import""SS#import<>S@implementationFractiondenominator:(int)d{S-(Fraction*)initWithNumerator:(int)nSself=[superinit];SSif(self){andDenominator:d];S[self setNumerator:nS}SSreturnself;S}and Settings\Administrator\ 桌 面 \Objective-C Begi...2020-2-6PDFcreatedwithpdfFactoryProtrialversion Page Objective-C Beginner's Guide頁碼,16/43SS -(void)print{S printf("%i/%i",numerator,denominator);S }SS -(void)setNumerator:(int)nS numerator=n;S }SS -(void)setDenominator:(int)dS denominator=d;S }SS -(void) setNumerator: (int) nandDenominator:(int)d{S numerator=n;S denominator=d;S }SS -(int)denominator{S returnS }SS -(int)numerator{S returnnumerator;S }@endnS #importSS @interfaceComplex:NSObjectS doublereal;S doubleimaginary;S }SS -(Complex*)initWithReal:(double)randImaginary:(double)i;S -(void)setReal:(double)r;S -(void)setImaginary:(double)i;S -(void) setReal: (double) randImaginary:(double)i;S -(double)real;S -(double)imaginary;S -(void)print;S@endnS #import""S #import<>and Settings\Administrator\ 桌 面 \Objective-C Begi...2020-2-6PDFcreatedwithpdfFactoryProtrialversion Page Objective-C Beginner's Guide頁碼,17/43SS @implementationComplexS -(Complex*)initWithReal:(double)randImaginary:(double)i{S self=[superinit];SS if(self){S andImaginary:i];S }SS returnself;SS}S-(void)setReal:(double)r{Sreal=r;S}SS-(void)setImaginary:(double)i{Simaginary=i;S}SandImaginary:(double)i{S-(void)setReal:(double)rSreal=r;Simaginary=i;S}SS-(double)real{Sreturnreal;S }SS -(double)imaginary{S returnimaginary;S }SS -(void)print{S printf("%_f+%_fi",real,imaginary);S }S@endnSSSS#import<>#import""#import""*argv[]){Sintmain(intargc,constcharS.2020-2-6PDFcreatedwithpdfFactoryProtrialversion Page Objective-C Beginner's Guide頁碼,18/43SS ntValue()呼那個(gè)method。o 繼承(Inheritance)n 基于"Programming in Objective-C," 2004bySamsPublishing一中的范例,并通過許諾而刊載。nS #importSS @interfaceRectangle:NSObjectS intwidth;S intheight;S }SS -(Rectangle*)initWithWidth:(int)wheight:(int)h;S -(void)setWidth:(int)w;S -(void)setHeight:(int)h;S -(void)setWidth:(int)wheight:(int)h;S -(int)width;S -(int)height;and Settings\Administrator\ 桌 面 \Objective-C Begi...2020-2-6PDFcreatedwithpdfFactoryProtrialversion Page Objective-C Beginner's Guide頁碼,19/43S -(void)print;@endnS #import""S #import<>SS @implementationRectangleS -(Rectangle*)initWithWidth:(int)wheight:(int)h{S self=[superinit];SS if(self){S [selfsetWidth:wheight:h];S }SS returnself;S }SS -(void)setWidth:(int)wS width=w;S }SS -(void)setHeight:(int)hS height=h;S }Sh{S-(void)setWidth:(int)wheight:(int)Swidth=w;Sheight=h;S}SS-(int)width{Sreturnwidth;S}SS-(int)height{Sreturn height;S}SS-(void)print{=%i",width,height);Sprintf("width=%i,heightS}@endnS #import""SS @interfaceSquare:Rectangleand Settings\Administrator\ 桌 面 \Objective-C Begi...2020-2-6PDFcreatedwithpdfFactoryProtrialversionObjective-C Beginner's Guide頁碼,20/43S -(Square*)initWithSize:(int)s;S -(void)setSize:(int)s;S -(int)size;@endnS #import""SS @implementationSquareS -(Square*)initWithSize:(int)s{S self=[superinit];SS if(self){S [selfsetSize:s];S }SS returnself;S }SS -(void)setSize:(int)sS width=s;S height=s;S }SS -(int)size{S returnwidth;S }SS -(void)setWidth:(int)wS [selfsetSize:w];S }SS -(void)setHeight:(int)hS [selfsetSize:h];S }@endnS #import""S #import""S #import<>SS intmain(intargc,constchar*argv[]){SS Square*sq=[[Squarealloc]initWithSize:15];SS .2020-2-6PDFcreatedwithpdfFactoryProtrialversion Page2 Objective-C Beginner's Guide頁碼,21/43S [recprint];S printf("\n");SS printf("Square:");S [sqprint];S printf("\n");SS . 2020-2-6PDFcreatedwithpdfFactoryProtrialversionObjective-C Beginner's Guide頁碼,22/43classObj-(BOOL) isMemberOfClass: classObj此對(duì)象是不是是classObj的一員doestheobjecthaveamethodnamedspecifiecbytheselector此對(duì)象是不是有叫做selector的methoddoesanobjectcreatedbythisclasshavetheabilitytorespondtothe

-(BOOL)respondsToSelector:selectorselector specifiedselector此對(duì)象是不是是由有能力響應(yīng)指定selector的對(duì)象所產(chǎn)生invokethespecifiedselectorontheobject

+(BOOL) instancesRespondToSelector:-(id)performSelector:selector喚起此對(duì)象的指定selectornNSObjectclassclassmethod。這超級(jí)近似于JavagetClass()methodclass對(duì)象被利用于前述的methods中。nSelectors在Objective-C用以表示訊息。下一個(gè)范例會(huì)秀出成立selector的語法。n 基于"Programming in Objective-C," 2004bySamsPublishing一中的范例,并通過許諾而刊載。nS #import""S #import""S #importSS intmain(intargc,constchar*argv[])SS Square*sq=[[Squarealloc]initWithSize:15];SS .2020-2-6PDFcreatedwithpdfFactoryProtrialversionObjective-C Beginner's Guide頁碼,23/43S if([sqisMemberOfClass:[NSObjectclass]]==YES){S printf("squareisamemberofobjectclass\n");S }SS .2020-2-6PDFcreatedwithpdfFactoryProtrialversionObjective-C Beginner's Guide頁碼,24/43S . 2020-2-6PDFcreatedwithpdfFactoryProtrialversionObjective-C Beginner's Guide頁碼,25/43SSS }SS -(Fraction*)div:(Fraction*)fSSS }SS -(Fraction*)sub:(Fraction*)fSSSS }@endnS #import<>S #import""S #import""SS intmain(intargc,constchar*argv[]){Sand Settings\Administrator\ 桌 面 \Objective-C Begi...2020-2-6PDFcreatedwithpdfFactoryProtrialversionObjective-C Beginner's Guide頁碼,26/43n (class)category,其它c(diǎn)ategories得加上不同的、唯一無的名字。nCategories在成立privatemethods時(shí)十分有效。因?yàn)镺bjective-C并無像Java這種private/protected/publicmethods的概念,因此必需要利用categories來達(dá)成這種功能。作法是把privatemethod從你的classheader(.h)檔案移到短的范例。

implementation(.m)檔案。以下是此種作法一個(gè)簡nS #importSS @interfaceMyClass:NSObjectS -(void)publicMethod;@endnS #import""S #importSS @implementationMyClassS -(void)publicMethod{S printf("publicmethod\n"S }S @endSS . 2020-2-6PDFcreatedwithpdfFactoryProtrialversionObjective-C Beginner's Guide頁碼,27/43S [objrelease];SS return0;}noutputpublicmethodo PosingnPosing有點(diǎn)像categories,可是不太一樣。它許諾你擴(kuò)充一個(gè)class,而且全面性地的扮演(pose)那個(gè)superclass。例如:你有一個(gè)擴(kuò)充NSArray的NSArrayChild物件。若是你讓NSArrayChild扮演NSArray,那么在你的程序代碼中所有的NSArray 都會(huì)自動(dòng)被替代為NSArrayChild。n 基于"Programming in Objective-C," 2004bySamsPublishing一中的范例,并通過許諾而刊載。nS #import""SS @interfaceFractionB:FractionS -(void)print;S @endnS #import""S #importSS @implementationFractionBS -(void)print{S printf("(%i/%i)",numerator,denominator);S }@endnS #import<>S #import""S #import""SS intmain(intargc,constchar*argv[])SSS .2020-2-6PDFcreatedwithpdfFactoryProtrialversionObjective-C Beginner's Guide頁碼,28/43S .2020-2-6PDFcreatedwithpdfFactoryProtrialversionObjective-C Beginner's Guide頁碼,29/43S -(Fraction*)initWithNumerator:(int)ndenominator:(int)d;S -(void)setNumerator:(int)d;S -(void)setDenominator:(int)d;S -(void) setNumerator: (int) nandDenominator:(int)d;S -(int)numerator;S -(int)denominator;@endnS #import""S #import<>SS @implementationFractionS -(Fraction*)initWithNumerator:(int)ndenominator:(int)d{S self=[superinit];SS if(self){S [selfsetNumerator:nandDenominator:d];S }SS returnself;S }SS -(void)print{S printf("%i/%i",numerator,denominator);S }SS -(void)setNumerator:(int)nS numerator=n;S }SS -(void)setDenominator:(int)dS denominator=d;S }SS -(void) setNumerator: (int) nandDenominator:(int)d{S numerator=n;S denominator=d;S }SS -(int)denominator{S returnS }SS -(int)numerator{S returnnumerator;S }SS -(Fraction*) copyWithZone:(NSZone*)zone{and Settings\Administrator\ 桌 面 \Objective-C Begi...2020-2-6PDFcreatedwithpdfFactoryProtrialversionObjective-C Beginner's Guide頁碼,30/43SSS }@endnS #import<Foundation/>S #import""SS @interfaceComplex:NSObject<Printing>{S doublereal;S doubleimaginary;S }SS -(Complex*)initWithReal:(double)randImaginary:(double)i;S -(void)setReal:(double)r;S -(void)setImaginary:(double)i;S -(void) setReal: (double) randImaginary:(double)i;S -(double)real;S -(double)imaginary;@endnS #import""S #import<>SS @implementationComplexS -(Complex*)initWithReal:(double)randImaginary:(double)i{S self=[superinit];SS if(self){S [self setReal:randImaginary:i];S }SS returnself;S }SS -(void)setReal:(double)rS real=r;S }SS -(void)setImaginary:(double)i{Simaginary=i;S}SSandImaginary:(double)i{-(void) setReal: (double) rSreal=r;Simaginary=i;and Settings\Administrator\ 桌 面 \Objective-C Begi...2020-2-6PDFcreatedwithpdfFactoryProtrialversion Page3 Objective-C Beginner's Guide頁碼,31/43S }SS -(double)real{S returnS }SS -(double)imaginary{S returnimaginary;S }SS -(void)print{S printf("%_f+%_fi",real,imaginary);S }@endnS #import<>S #import""S #import""SS intmain(intargc,constchar*argv[]){S .2020-2-6PDFcreatedwithpdfFactoryProtrialversionObjective-C Beginner's Guide頁碼,32/43Sprintf("FractionconformstoNSCopying\n");SSS}.2020-2-6PDFcreatedwithpdfFactoryProtrialversionObjective-C Beginner's Guide頁碼,33/43o RetainandRelease (保留與釋放)nRetain releaseNSObject都會(huì)有的methods。每一個(gè)對(duì)reference

象都有一個(gè)內(nèi)部計(jì)數(shù)器,能夠用來追蹤對(duì)象的reference dealloc0個(gè) 時(shí)不需要 自己可是若是計(jì)數(shù)器值抵達(dá)時(shí),對(duì)象就得dealloc 自己[objectretain]會(huì)將計(jì)數(shù)器值加1(值從1開始,[objectrelease]那么將計(jì)數(shù)器值減 1。若是呼release]致使計(jì)數(shù)器抵達(dá)0,就會(huì)自動(dòng)dealloc。nS...S -(void)dealloc{S printf("Deallocingfraction\n");S [superdealloc];S }...n 基于"Programming in Objective-C," 2004bySamsPublishing一中的范例,并通過許諾而刊載。nS #import""S #import<>SS intmain(intargc,constchar*argv[]){S Fraction*frac1=[[Fractionalloc]init];S Fraction*frac2=[[Fractionalloc]init];SS .2020-2-6PDFcreatedwithpdfFactoryProtrialversionObjective-C Beginner's Guide頁碼,34/43S [frac1 release]; .2020-2-6PDFcreatedwithpdfFactoryProtrialversionObjective-C Beginner's Guide頁碼,35/43S email:(NSString*)e;S -(void)setFirst:(NSString*)flast:(NSString*)l;S -(void)print;@endnS #import""S #import<>SS @implementationAddressCardS -(AddressCard*) initWithFirst:(NSString*)fS last:(NSString*)lS email:(NSString*)e{S self=[superinit];SS if(self){S [selfsetFirst:flast:lemail:e];S }SS returnself;S }SS -(NSString*)firstS returnfirst;S }SS -(NSString*)lastS returnlast;S }SS -(NSString*)emailS returnemail;S }SS -(void)setFirst:(NSString*)fS [fretain];S [firstrelease];S first=f;S }SS -(void)setLast:(NSString*)lS [lretain];S [lastrelease];S last=l;S }SS -(void)setEmail:(NSString*)e{S [eretain];S [emailrelease];and Settings\Administrator\ 桌 面 \Objective-C Begi...2020-2-6PDFcreatedwithpdfFactoryProtrialversionObjective-C Beginner's Guide頁碼,36/43S email=e;S }SS -(void)setFirst:(NSString*)fS last:(NSString*)lS email:(NSString*)e{S [selfsetFirst:f];S [selfsetLast:l];S [selfsetEmail:S }SS -(void)setFirst:(NSString*)flast:(NSString*)l{S [selfsetFirst:f];S [selfsetLast:l];S }SS -(void)print{S printf("%s%s<%s>",[firstcString],S[lastcString],S[emailcString]);S }SS -(void)dealloc{S [firstrelease];S [lastrelease];S [emailrelease];SS [superdealloc];S }@endnS #import""S #import<Foundation/>S #importSS intmain(intargc,constchar*argv[]){S NSString*first=[[NSStringalloc]initWithCString:"Tom"];S NSString*last=[[NSStringalloc]initWithCString:"Jones"];S NSString*email=[[NSStringalloc]initWithCString:"S AddressCard *tom =[[AddressCardalloc]initWithFirst:firstSlast:lastSemail:email];SS .2020-2-6PDFcreatedwithpdfFactoryProtrialversionObjective-C Beginner's Guide頁碼,37/43S . 2020-2-6PDFcreatedwithpdfFactoryProtrialversionObjective-C Beginner's Guide頁碼,38/43SSS .S +(Fraction*)fractionWithNumerator:(int)ndenominator:(int)d;S ...nS...S +(SS [retautorelease];SS returnret;S }...and Settings\Administrator\ 桌 面 \Objective-C Begi...2020-2-6PDFcreatedwithpdfFactoryProtrialversionObjective-C Beginner's Guide頁碼,39/43nS #import<Foundation/>S #import""S #importSS intmain(intargc,constchar*argv[]){S NSAutoreleasePool*pool [[NSAutoreleasePoolalloc]init];SSSS . 2020-2-6PDFcreatedwithpdfFactoryProtrialversionObjective-C Beginner's Guide頁碼,40/43Foundationframeworkclasses°Foundationframework地位猶如C++StandardTemplateLibraryObjective-C是真正的動(dòng)態(tài)識(shí)別語言(dynamictypesC++那樣肥得恐怖的樣版(templatesframework執(zhí)行緒,還有更多好東西。o NSArrayn 基于"Programming in Objective-C," 2004bySamsPublishing一中的范例,并通過許諾而刊載。nS #import<Foundation/>S #import<Foundation/>S #import<Foundation/>S #import

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論