ImageVerifierCode 换一换
格式:DOC , 页数:51 ,大小:247.50KB ,
资源ID:8728944      下载积分:10 金币
验证码下载
登录下载
邮箱/手机:
图形码:
验证码: 获取验证码
温馨提示:
支付成功后,系统会自动生成账号(用户名为邮箱或者手机号,密码是验证码),方便下次登录下载和查询订单;
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

开通VIP
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.zixin.com.cn/docdown/8728944.html】到电脑端继续下载(重复下载【60天内】不扣币)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

开通VIP折扣优惠下载文档

            查看会员权益                  [ 下载后找不到文档?]

填表反馈(24小时):  下载求助     关注领币    退款申请

开具发票请登录PC端进行申请。


权利声明

1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前可先查看【教您几个在下载文档中可以更好的避免被坑】。
5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
6、文档遇到问题,请及时联系平台进行协调解决,联系【微信客服】、【QQ客服】,若有其他问题请点击或扫码反馈【服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【版权申诉】”,意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:4009-655-100;投诉/维权电话:18658249818。

注意事项

本文(Object-C 经典入门教程.doc)为本站上传会员【s4****5z】主动上传,咨信网仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知咨信网(发送邮件至1219186828@qq.com、拔打电话4009-655-100或【 微信客服】、【 QQ客服】),核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载【60天内】不扣币。 服务填表

Object-C 经典入门教程.doc

1、 Object-C 入门教程 分类: Sip&asterisk2009-05-04 16:34 16409人阅读 评论(2) 收藏 举报 大纲 o 开始吧下载这篇教学 o 设定环境 o 前言 o 编译 hello world o 创建 Classes@interface o @implementation o 把它们凑在一起 o 详细说明...多重参数 o 建构子(Constructors) o 访问权限 o Class level access o 异常情况(Exceptions)处理 o 继承、多型(Inheritance, Polymorphism)

2、以及其他面向对象功能id 型别 o 继承(Inheritance) o 动态识别(Dynamic types) o Categories o Posing o Protocols o 内存管理Retain and Release(保留与释放) o Dealloc o Autorelease Pool o Foundation Framework ClassesNSArray o NSDictionary · 优点与缺点 · 更多信息 开始吧 下载这篇教学 § 所有这篇初学者指南的原始码都可以由 objc.tar.gz 下载。这篇教学中的许多范例都是由 Stev

3、e Kochan 在 Programming in Objective-C. 一书中撰写。如果你想得到更多详细信息及范例,请直接参考该书。这个网站上登载的所有范例皆经过他的允许,所以请勿复制转载。 设定环境 § Linux/FreeBSD: 安装 GNUStep为了编译 GNUstep 应用程序,必须先执行位于/usr/GNUstep/System/Makefiles/GNUstep.sh 的 GNUstep.sh 这个档案。这个路径取决于你的系统环境,有些是在 /usr, some /usr/lib,有些是/usr/local。如果你的 shell 是以 csh/tcsh 为基础的 sh

4、ell,则应该改用 GNUStep.csh。建议把这个指令放在 .bashrc 或 .cshrc 中。 § Mac OS X: 安装 XCode § Windows NT 5.X: 安装 cygwin 或 mingw,然后安装 GNUStep 前言 § 这篇教学假设你已经有一些基本的 C 语言知识,包括 C 数据型别、什么是函式、什么是回传值、关于指针的知识以及基本的 C 语言内存管理。如果您没有这些背景知识,我非常建议你读一读 K&R 的书:The C Programming Language(译注:台湾出版书名为 C 程序语言第二版)这是 C 语言的设计者所写的书。 § Obje

5、ctive-C,是 C 的衍生语言,继承了所有 C 语言的特性。是有一些例外,但是它们不是继承于 C 的语言特性本身。 § nil:在 C/C++ 你或许曾使用过 NULL,而在 Objective-C 中则是 nil。不同之处是你可以传递讯息给 nil(例如 [nil message];),这是完全合法的,然而你却不能对 NULL 如法炮制。 § BOOL:C 没有正式的布尔型别,而在 Objective-C 中也不是「真的」有。它是包含在 Foundation classes(基本类别库)中(即 import NSObject.h;nil 也是包括在这个头文件内)。BOOL 在 Obj

6、ective-C 中有两种型态:YES 或 NO,而不是 TRUE 或 FALSE。 § #import vs #include:就如同你在 hello world 范例中看到的,我们使用了#import。#import 由 gcc 编译程序支援。我并不建议使用 #include,#import基本上跟 .h 档头尾的 #ifndef #define #endif 相同。许多程序员们都同意,使用这些东西这是十分愚蠢的。无论如何,使用 #import 就对了。这样不但可以避免麻烦,而且万一有一天 gcc 把它拿掉了,将会有足够的 Objective-C 程序员可以坚持保留它或是将它放回来。偷偷

7、告诉你,Apple 在它们官方的程序代码中也使用了 #import。所以万一有一天这种事真的发生,不难预料 Apple 将会提供一个支持 #import 的 gcc 分支版本。 § 在 Objective-C 中, method 及 message 这两个字是可以互换的。不过messages 拥有特别的特性,一个 message 可以动态的转送给另一个对象。在Objective-C 中,呼叫对象上的一个讯息并不一定表示对象真的会实作这个讯息,而是对象知道如何以某种方式去实作它,或是转送给知道如何实作的对象。 编译 hello world § hello.m § #import

8、dio.h> § § int main( int argc, const char *argv[] ) { § printf( "hello world/n" ); § return 0; } · o § 输出 hello world · o § 在 Objective-C 中使用 #import 代替 #include § Objective-C 的预设扩展名是 .m 创建 classes @interface § 基于 "Programming in Objective-C," Copyright © 2004 b

9、y Sams Publishing一书中的范例,并经过允许而刊载。 § Fraction.h § #import § § @interface Fraction: NSObject { § int numerator; § int denominator; § } § § -(void) print; § -(void) setNumerator: (int) d; § -(void) setDenominator: (int) d; § -(int) numerator;

10、§ -(int) denominator; § @end · o § NSObject:NeXTStep Object 的缩写。因为它已经改名为 OpenStep,所以这在今天已经不是那么有意义了。 § 继承(inheritance)以 Class: Parent 表示,就像上面的 Fraction: NSObject。 § 夹在 @interface Class: Parent { .... } 中的称为 instance variables。 § 没有设定访问权限(protected, public, private)时,预设的访问权限为protected。设定权

11、限的方式将在稍后说明。 § Instance methods 跟在成员变数(即 instance variables)后。格式为:scope (returnType) methodName: (parameter1Type) parameter1Name;scope 有class 或 instance 两种。instance methods 以 - 开头,class level methods 以 + 开头。 § Interface 以一个 @end 作为结束。 @implementation § 基于 "Programming in Objective-C," Copyright ©

12、 2004 by Sams Publishing一书中的范例,并经过允许而刊载。 § Fraction.m § #import "Fraction.h" § #import § § @implementation Fraction § -(void) print { § printf( "%i/%i", numerator, denominator ); § } § § -(void) setNumerator: (int) n { § numerator = n; § } § § -(voi

13、d) setDenominator: (int) d { § denominator = d; § } § § -(int) denominator { § return denominator; § } § § -(int) numerator { § return numerator; § } @end · o § Implementation 以 @implementation ClassName 开始,以 @end 结束。 § Implement 定义好的 methods 的方式,跟在 inter

14、face 中宣告时很近似。 把它们凑在一起 § 基于 "Programming in Objective-C," Copyright © 2004 by Sams Publishing一书中的范例,并经过允许而刊载。 § main.m § #import § #import "Fraction.m" § § int main( int argc, const char *argv[] ) { § // create a new instance § Fraction *frac = [[Fraction alloc] ini

15、t]; § § // set the values § [frac setNumerator: 1]; § [frac setDenominator: 3]; § § // print it § printf( "The fraction is: " ); § [frac print]; § printf( "/n" ); § § // free memory § [frac release]; § § return 0; } · o

16、 § output The fraction is: 1/3 · o § Fraction *frac = [[Fraction alloc] init];这行程序代码中有很多重要的东西。 § 在 Objective-C 中呼叫 methods 的方法是 [object method],就像 C++的 object->method()。 § Objective-C 没有 value 型别。所以没有像 C++ 的 Fraction frac; frac.print(); 这类的东西。在 Objective-C 中完全使用指针来处理对象。 § 这行程序代码实际上做了两件

17、事: [Fraction alloc] 呼叫了 Fraction class 的 alloc method。这就像 malloc 内存,这个动作也做了一样的事情。 § [object init] 是一个建构子(constructor)呼叫,负责初始化对象中的所有变量。它呼叫了 [Fraction alloc] 传回的 instance 上的 init method。这个动作非常普遍,所以通常以一行程序完成:Object *var = [[Object alloc] init]; § [frac setNumerator: 1] 非常简单。它呼叫了 frac 上的 setNumerator

18、method并传入 1 为参数。 § 如同每个 C 的变体,Objective-C 也有一个用以释放内存的方式: release。它继承自 NSObject,这个 method 在之后会有详尽的解说。 详细说明... 多重参数 § 目前为止我还没展示如何传递多个参数。这个语法乍看之下不是很直觉,不过它却是来自一个十分受欢迎的 Smalltalk 版本。 § 基于 "Programming in Objective-C," Copyright © 2004 by Sams Publishing一书中的范例,并经过允许而刊载。 § Fraction.h § ... § -(vo

19、id) setNumerator: (int) n andDenominator: (int) d; ... · o § Fraction.m § ... § -(void) setNumerator: (int) n andDenominator: (int) d { § numerator = n; § denominator = d; § } ... · o § main.m § #import § #import "Fraction.h" § § int main( int ar

20、gc, const char *argv[] ) { § // create a new instance § Fraction *frac = [[Fraction alloc] init]; § Fraction *frac2 = [[Fraction alloc] init]; § § // set the values § [frac setNumerator: 1]; § [frac setDenominator: 3]; § § // combined set § [fr

21、ac2 setNumerator: 1 andDenominator: 5]; § § // print it § printf( "The fraction is: " ); § [frac print]; § printf( "/n" ); § § // print it § printf( "Fraction 2 is: " ); § [frac2 print]; § printf( "/n" ); § § // free memory § [f

22、rac release]; § [frac2 release]; § § return 0; } · o § output § The fraction is: 1/3 Fraction 2 is: 1/5 · o § 这个 method 实际上叫做 setNumerator:andDenominator: § 加入其他参数的方法就跟加入第二个时一样,即 method:label1:label2:label3:,而呼叫的方法是 [obj method: param1 label1: param2 label2: param3

23、 label3: param4] § Labels 是非必要的,所以可以有一个像这样的 method:method:::,简单的省略label 名称,但以 : 区隔参数。并不建议这样使用。 建构子(Constructors) § 基于 "Programming in Objective-C," Copyright © 2004 by Sams Publishing一书中的范例,并经过允许而刊载。 § Fraction.h § ... § -(Fraction*) initWithNumerator: (int) n denominator: (int) d; ... ·

24、 o § Fraction.m § ... § -(Fraction*) initWithNumerator: (int) n denominator: (int) d { § self = [super init]; § § if ( self ) { § [self setNumerator: n andDenominator: d]; § } § § return self; § } ... · o § main.m § #import §

25、import "Fraction.h" § § int main( int argc, const char *argv[] ) { § // create a new instance § Fraction *frac = [[Fraction alloc] init]; § Fraction *frac2 = [[Fraction alloc] init]; § Fraction *frac3 = [[Fraction alloc] initWithNumerator: 3 denominator: 10]; § §

26、 // set the values § [frac setNumerator: 1]; § [frac setDenominator: 3]; § § // combined set § [frac2 setNumerator: 1 andDenominator: 5]; § § // print it § printf( "The fraction is: " ); § [frac print]; § printf( "/n" ); § § print

27、f( "Fraction 2 is: " ); § [frac2 print]; § printf( "/n" ); § § printf( "Fraction 3 is: " ); § [frac3 print]; § printf( "/n" ); § § // free memory § [frac release]; § [frac2 release]; § [frac3 release]; § § return 0; } · o

28、 § output § The fraction is: 1/3 § Fraction 2 is: 1/5 Fraction 3 is: 3/10 · o § @interface 里的宣告就如同正常的函式。 § @implementation 使用了一个新的关键词:super如同 Java,Objective-C 只有一个 parent class(父类别)。 § 使用 [super init] 来存取 Super constructor,这个动作需要适当的继承设计。 § 你将这个动作回传的 instance 指派给另一新个关键词:self。Self 很像C+

29、 与 Java 的 this 指标。 § if ( self ) 跟 ( self != nil ) 一样,是为了确定 super constructor 成功传回了一个新对象。nil 是 Objective-C 用来表达 C/C++ 中 NULL 的方式,可以引入 NSObject 来取得。 § 当你初始化变量以后,你用传回 self 的方式来传回自己的地址。 § 预设的建构子是 -(id) init。 § 技术上来说,Objective-C 中的建构子就是一个 "init" method,而不像 C++ 与Java 有特殊的结构。 访问权限 § 预设的权限是 @protect

30、ed § Java 实作的方式是在 methods 与变量前面加上 public/private/protected 修饰语,而 Objective-C 的作法则更像 C++ 对于 instance variable(译注:C++术语一般称为 data members)的方式。 § Access.h § #import § § @interface Access: NSObject { § @public § int publicVar; § @private § int privateVar;

31、 § int privateVar2; § @protected § int protectedVar; § } @end · o § Access.m § #import "Access.h" § § @implementation Access @end · o § main.m § #import "Access.h" § #import § § int main( int argc, const char *argv[] ) { § Access *a =

32、 [[Access alloc] init]; § § // works § a->publicVar = 5; § printf( "public var: %i/n", a->publicVar ); § § // doesn't compile § //a->privateVar = 10; § //printf( "private var: %i/n", a->privateVar ); § § [a release]; § return 0; } · o

33、 § output public var: 5 · o § 如同你所看到的,就像 C++ 中 private: [list of vars] public: [list of vars] 的格式,它只是改成了@private, @protected, 等等。 Class level access § 当你想计算一个对象被 instance 几次时,通常有 class level variables 以及class level functions 是件方便的事。 § ClassA.h § #import §

34、§ static int count; § § @interface ClassA: NSObject § +(int) initCount; § +(void) initialize; @end · o § ClassA.m § #import "ClassA.h" § § @implementation ClassA § -(id) init { § self = [super init]; § count++; § return self; § } § § +(int) init

35、Count { § return count; § } § § +(void) initialize { § count = 0; § } @end · o § main.m § #import "ClassA.h" § #import § § int main( int argc, const char *argv[] ) { § ClassA *c1 = [[ClassA alloc] init]; § ClassA *c2 = [[ClassA alloc] ini

36、t]; § § // print count § printf( "ClassA count: %i/n", [ClassA initCount] ); § § ClassA *c3 = [[ClassA alloc] init]; § § // print count again § printf( "ClassA count: %i/n", [ClassA initCount] ); § § [c1 release]; § [c2 release]; § [

37、c3 release]; § § return 0; } · o § output § ClassA count: 2 ClassA count: 3 · o § static int count = 0; 这是 class variable 宣告的方式。其实这种变量摆在这里并不理想,比较好的解法是像 Java 实作 static class variables 的方法。然而,它确实能用。 § +(int) initCount; 这是回传 count 值的实际 method。请注意这细微的差别!这里在 type 前面不用减号 -

38、而改用加号 +。加号 + 表示这是一个 class level function。(译注:许多文件中,class level functions 被称为 class functions 或 class method) § 存取这个变数跟存取一般成员变数没有两样,就像 ClassA 中的 count++ 用法。 § +(void) initialize method is 在 Objective-C 开始执行你的程序时被呼叫,而且它也被每个 class 呼叫。这是初始化像我们的 count 这类 class level variables 的好地方。 异常情况(Exceptions) §

39、 注意:异常处理只有 Mac OS X 10.3 以上才支持。 § 基于 "Programming in Objective-C," Copyright © 2004 by Sams Publishing一书中的范例,并经过允许而刊载。 § CupWarningException.h § #import § § @interface CupWarningException: NSException @end · o § CupWarningException.m § #import "CupWarni

40、ngException.h" § § @implementation CupWarningException @end · o § CupOverflowException.h § #import § § @interface CupOverflowException: NSException @end · o § CupOverflowException.m § #import "CupOverflowException.h" § § @implementatio

41、n CupOverflowException @end · o § Cup.h § #import § § @interface Cup: NSObject { § int level; § } § § -(int) level; § -(void) setLevel: (int) l; § -(void) fill; § -(void) empty; § -(void) print; @end · o § Cup.m § #import "Cup.h

42、" § #import "CupOverflowException.h" § #import "CupWarningException.h" § #import § #import § § @implementation Cup § -(id) init { § self = [super init]; § § if ( self ) { § [self setLevel: 0]; § } §

43、 § return self; § } § § -(int) level { § return level; § } § § -(void) setLevel: (int) l { § level = l; § § if ( level > 100 ) { § // throw overflow § NSException *e = [CupOverflowException § exceptionWithName: @"CupOverf

44、lowException" § reason: @"The level is above 100" § userInfo: nil]; § @throw e; § } else if ( level >= 50 ) { § // throw warning § NSException *e = [CupWarningException § exceptionWithName: @"CupWarningException" §

45、 reason: @"The level is above or at 50" § userInfo: nil]; § @throw e; § } else if ( level < 0 ) { § // throw exception § NSException *e = [NSException § exceptionWithName: @"CupUnderflowException" § reason: @"The lev

46、el is below 0" § userInfo: nil]; § @throw e; § } § } § § -(void) fill { § [self setLevel: level + 10]; § } § § -(void) empty { § [self setLevel: level - 10]; § } § § -(void) print { § printf( "Cup level is: %i/n", level ); § }

47、 @end · o § main.m § #import "Cup.h" § #import "CupOverflowException.h" § #import "CupWarningException.h" § #import § #import § #import § #import § § int main( int argc, const ch

48、ar *argv[] ) { § NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; § Cup *cup = [[Cup alloc] init]; § int i; § § // this will work § for ( i = 0; i < 4; i++ ) { § [cup fill]; § [cup print]; § } § § // this will throw exc

49、eptions § for ( i = 0; i < 7; i++ ) { § @try { § [cup fill]; § } @catch ( CupWarningException *e ) { § printf( "%s: ", [[e name] cString] ); § } @catch ( CupOverflowException *e ) { § printf( "%s: ", [[e name] cString] );

50、 § } @finally { § [cup print]; § } § } § § // throw a generic exception § @try { § [cup setLevel: -1]; § } @catch ( NSException *e ) { § printf( "%s: %s/n", [[e name] cString], [[e reason] cString] ); § } § §

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服