版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、毕业设计外文资料翻译学 院: 专 业: 姓 名: 学 号: 外文出处: BEGINNING iOS GAME DEVELOPMENT 附 件:1.外文资料翻译译文; 2.外文原文 指导教师评语:该生的外文翻译与本次的设计研究内容有一定的关联性,翻译文章通顺,语法正确,工作量也达到了要求的内容。 翻译工作能够严格按照规定,达到考核、提高学生英语应用水平的目的,圆满完成要求。签名: 2015年10月14日1外文资料翻译译文objective - c编程语言 objective C是C语言的超集,一切你可以在C语言中,你也可以做在objective - C中。objective - C的大多数特性增
2、加了C程序使您能够构建利用面向对象(OO)的编程技术。在最后一章中,您了解到C是struc -约束编程语言。结构化语言运行以自顶向下的方式,鼓励按部就班,基于函数的方法来解决问题。 另一方面,面向对象的语言鼓励更抽象的方法,是你将在本章中学习到的。在objective - c等面向对象的语言,你的软件作为一个群体的对象模型。而不是写功s能做一个特定的操作,你将你的程序逻辑封装到这些对象。对象的功能,或方法,操作对象中包含的数据。对象也可以包含自己的数据。通常,您将模型中的对象程序基于现实世界的对象。这可以使面向对象的程序更容易理解。面向对象的程序也可以更容易调试和修改,因为你封装所有相关的功能
3、和数据对象在对象本身。类和对象 功能面向对象程序的基本单位是对象。当你的程序运行时,可能有数百个,也许成千上百万的对象活跃在任何给定的时间。你在项目中创建的所有对象都是来自于类。 类是创建对象的蓝图。就像一个构建器使用一个蓝图来创建房屋,您使用类来创建对象。每个房子都可能有不同的颜色,但是每个遵循相同的蓝色打印。在图4 - 1中,您可以看到蓝图一所房子,那所房子的三个实例:一个红色,一个绿色,一个黄色。构建器使每个房子的蓝图,但改变了颜色。你能想到的颜色作为房子的一个属性。每个房子都有一个颜色,但是你可以设置颜色为每个实例为单独的房子。图4 - 1:建造房屋的蓝图 在你的代码中,您定义的类表示
4、的对象,将使用您的程序。然后,当您需要使用一个对象,您将创建您的类的一个实例。类的实例,您已经创建了被称为一个对象。一旦您创建了一个对象,它是独立的,除了相同的类型,它不与任何其他对象创建的类。回到家里类比,每个房子都是一样的类型,但是您可以独立改变任何房子的颜色而不影响其他的房子。你可以创建一个房子类代表房子在您的代码中。模型说明在图4 - 1中,众议院类成员变量会有颜色。然后,你可以创建你想要尽可能多的房子的实例,每个都有自己的颜色。改变一个房子对象的颜色没有影响您创建的其他房子。实例变量 成员或实例变量维护对象的当前状态或条件。您在成员变量的值独立地存储在每一个类的实例。正如您将看到的,
5、每一个类型的对象创建的房子会有自己独特的值存储在实例变量的颜色。您可以访问类的成员变量来实现。 实例变量有不同的范围,这取决于你如何定义他们。这三个范围值,您将使用最常private,protected, public。私有实例变量只提供给他们声明的类。保护可用变量是类的声明和任何子类。你会了解更多关于子类在本章后面。公共变量可用于任何代码。默认情况下,实例变量都有受保护的范围。 你可以看到这个如图4 - 2所示。图中描绘了一个成立这个在不同课程中声明的变量范围的水平。一个私有变量的可见性,以绿色显示,成立这个。课程只可见保护范围,见黄色,表明变量成立这个和任何课程都可用这个。课程类继承自最后
6、,公众范围所示粉红色。任何代码可以访问公共实例变量。 面向对象程序的一个优点是封装。封装的概念是你可以包含所有功能的类的类本身没有暴露的内部细节用户类。这样,类看起来像一个黑盒,外面的世界。你们班的用户应该不需要知道他们也不应该关心如何实现类。你可以隐藏你的类和实现的内部细节封装使用私有或受保护的成员变量。这样,您的类的用户不能直接访问类的成员。所有访问应该通过消息或属性。你会了解更多关于属性在本章后面。通过使用这种方法,您可以控制用户如何获取数据从您的类,而不是用户直接内部数据。图4 - 2:成员变量范围内存管理 与垃圾收集语言不同,像Java和c#,objective - C不提供垃圾收集
7、在iOS平台上。因此,当你为创建的对象分配内存,你负责释放内存,当你完成它。如果不能正确地分配释放内存,您的应用程序所消耗的内存的总量将增长作为应用程序运行。这未能清理未使用的内存导致内存泄漏。最后,如果您的程序使用太多内存,操作系统将终止您的应用程序。 这似乎困难和复杂,但只要你有在objective - c中内存管理的工作方式的理解和记住一些简单的规则,你就会好起来的。在iOS 5,苹果公司推出了一种新的语言特性称为自动引用计数的objective - c,ARC。ARC简化了引用计数通过消除大部分为开发加工工作,它仍然是重要的有一个基本的了解引用计数在objective - c中工作。在
8、下一节中您将了解更多关于ARC。 所有继承NSObject objective - c类保持保留计数。保留计数是一个计数器,显示的代码的比特数感兴趣的对象。当一个对象分配利用alloc或新,其保留数设置为1。在图4 - 3中,你的对象由蓝色方块表示,保留计数在粉红色的圆。当你用完对象后,你应该调用释放的方法。调用对象的释放减少保留计数。保留计数为0时,对象被回收和objective - c运行时调用它的dealloc方法。你可以看到如图4 3(a)所示。灰色的方块代表了回收对象。 除了释放,你可以调用生成方法。这表明,运行时应释放对象在将来的一个点,但不是马上。有效地,这减少了保留数1就像调用
9、释放。你可以看到如图4 - 3(b)所示。ss 生成自动释放池来跟踪所有生成对象。生成自动有释放池,大多数项目模板创建时自动开始构建您的应用程序。你可以自己创建本地池。您将看到autorelease池在动作游戏中例子在这一章的结束。 对于从方法返回对象Autorelease尤其有用。你可以分配的对象你计划返回的方法,生成自动配置它,然后它返回给调用者。是调用者的责任保留对象。objective - c运行时将发送一个生成对象释放消息一次每次调用autorelease时autorelease池排水或回收。 当你需要坚持引用一个对象,就像一个生成对象,你从一个方法调用回来,你增加保留数通过调用保留
10、对象上的方法。图4 - 3(c)演示了这种情况。有责任的一个方法的调用者返回一个对象叫保留。默认释放池将取消生成对象通过应用程序的运行循环时间。所以,如果你得到一个生成对象从一个函数,使用它,你不需要它方法调用完成后,您不需要担心保留它。然而,如果你打算把一个生成对象实例变量的访问在稍后的时间,你必须打电话给保留它,否则它将回收的循环运行,当你试图回收对象发送一条消息时应用程序会崩溃。 你应该尽量使用生成自动释放,而不是在调用释放较少的开销。如果你要创建和生成许多对象,在一个循环中,或许你应该生成自动包装的循环池。这可以通过创建NSAutoreleasePool的实例对象在你开始循环时。然后,
11、你通过autorelease池释放它:NSAutoreleasePool *pool = NSAutoreleasePool alloc init; loop a bunch of times pool release; 要记住的关键一点是内存管理在objective - c中,你需要平衡调用新的alloc,或保留与调用发布或生成。如果你有太少的呼吁释放/生成,保留计数的对象永远不会下降到0,对象不会销毁,导致内存泄漏。如果你调用生成自动释放或太多次,你会over-release对象,导致应用程序崩溃。 一般来说,将一个对象添加到可可收集NSMutableArray等增量保留计数。同样,删除一
12、个对象从一个集合的精神性保留计数。仅仅获得一个对象的集合通常返回一个生成对象。如果你需要持有一个生成对象的引用,您需要调用保留。你将学习更多关于使用Cocoa在下一章集合类。 当你创建一个对象通过调用一个类alloc助手方法,新的、或复制它的名字,它是你的责任来释放该对象。以这种方式创建的对象,按照惯例,保留计数为1。如果你得到一个对象使用一个类的实例帮助器方法,它返回一个对象,没有alloc,新的,在它的名字如stringWithString或复制,你应该假设对象生成。 retainCount消息可以发送到任何NSObject获取当前保留计数的对象。你通常不会使用这种方法在生产应用程序,但它
13、可以帮助日志对象的保留计数当你试图调试内存问题。总结:如果你创建一个对象使用,新的、或复制,对象将保留数1,你负责调用释放。如果你得到一个对象的引用在任何其他方式,你可以假设它有一个保留数1和已经生成。如果你需要坚持的引用对象,保留它。如果你打电话给保留一个对象,你必须平衡叫保留与释放的电话。在内存管理的更多信息,我建议阅读苹果的内存管理编程指南自动引用计数 正如我在前一节中提到的,苹果iOS 5引入了自动引用计数或ARC。ARC简化了引用计数通过编译器插入自动保留并发布到您的代码。通过分析代码和编译器,这在编译时确定对象的生命周期。 ARC的影响在你作为一个开发者,你不需要担心打字保留,释放
14、,或在您的代码生成。编译器会插入正确的内存管理,编制报表给你。你不需要担心你的对象的生命周期,因为编译器会为你照顾它。事实上,生成自动调用保留,释放,或将导致编译器错误如果你有ARC为项目启用。 Xcode使ARC默认为所有新项目在4.2版开始。你可以关掉ARC与基础上如果你决定你想要完全控制内存管理。在这种情况下,您将需要恢复到使用保留和释放我之前解释道。这个特性允许您在您的新项目中使用ARC时保持兼容现有的库没有ARC启用。 因为Xcode使ARC默认为新项目,我将在本书中使用它。然而,你需要知道,如果你禁用ARC,您将需要使用保留自己管理内存和释放。2.外文原文The Objective
15、-C Programming Language Objective-C is a superset of C. Everything that you can do in C, you can also do in Objective-C. The majority of features that Objective-C adds to C enable you to build your programs by using object-oriented (OO) programming techniques. In the last chapter, you learned that C
16、 is a struc- tured programming language. Structured languages run in a top down manner and encourage a step-by-step, function-based approach to solving problems. On the other hand, object-oriented languages encourage a more abstract approach, as youwill learn in this chapter. In an object-oriented l
17、anguage like Objective-C, you model your software as a group of objects. Instead of writing functions that do a particular operation, you encapsulate your program logic into these objects. The objects have functions, or methods, that operate on the data contained in the objects. The objects can also
18、 contain their own data. Often, you will model the objects in your programs based on real-world objects. This can make object-oriented programs easier to understand. Object-oriented programs can also be easier to debug and modify because you encapsulate all of the functionality and data related to a
19、n object in the object itself. CLASSES AND OBJECTS The basic unit of functionality in an object-oriented program is the object. When your program runs, there may be hundreds, thousands, or perhaps millions of objects active at any given time. You create all of the objects in your program from classe
20、s. A class is the blueprint for creating objects. Just as a builder uses a blueprint to create houses, you use classes to create objects. Each house may have a different color, but each follows the same blue- print. In Figure 4-1, you can see the blueprint for a house and three instances of that hou
21、se: one red, one green, and one yellow. The builder made each house from the same blueprint, but changed the color. You can think of color as a property of a house. Each house has a color, but you can set the color for each instance of a house individually. FIGURE 4-1: Building houses from a bluepri
22、ntIn your code, you define classes that represent the kinds of objects that you will use in your program. Then, when you need to use an object, you create an instance of your class. The instance of the class that you have created is called an object. Once you have created an object, it stands alone
23、in that, aside from being of the same type, it is not associated with any other objects that you have created from the class. To go back to the house analogy, each house is the same type, but you can independently change the color of any house without affecting any other house. You could create a Ho
24、use class to represent houses in your code. To model the illustration in Figure 4-1, the House class would have a color member variable. Then, you could create as many instances of the House as you want, each with its own color. Changing the color of one house object has no effect on any other house
25、s that you created. Instance Variables Member or instance variables maintain the state or current condition of your objects. The values that you place in member variables are stored independently for each instance of a class. As you will see, each object of type House that you create will have its o
26、wn unique value stored in the color instance variable. You can access the member variables of your class in the implementation. Instance variables have a different scope depending on how you define them. The three scope values that you will use most often are private, protected, and public. Private
27、instance variables are available only to the class in which they are declared. Protected variables are available to the class in which they are declared and any subclasses. You will learn more about subclasses later in this chapter. Public variables are available from any code. By default, instance
28、variables have protected scope. You can see this illustrated in Figure 4-2. The diagram depicts a variable declared in theClass at the various scope levels. The visibility of a private variable, shown in green, is only visible to theClass. Protected scope, illustrated in yellow, shows that the varia
29、ble is available to both theClass and any class that inherits from theClass. Finally, public scope is shown in pink. Any code can access public instance variables. One advantage of an object-oriented program isencapsulation. The concept of encapsulation is that youcan contain all of the functionalit
30、y of a class in the classitself without exposing the inner details to users ofthe class. That way, the class looks like a black box to theoutside world. Users of your class should not need to know nor should they care about how you implemented the class. You can hide the internal details of your cla
31、ss and achieve encapsulation by using private or protected member variables. That way, users of your class cannot access a classs members directly. All access should be through messages or properties. You will learn more about properties later in this chapter. By using this methodology, you can cont
32、rol how users get data from your class as opposed to users going directly to the internal data. FIGURE 4-2: Member variable scopeManaging Memory Unlike garbage-collected languages like Java and C#, Objective-C does not offer garbage collection on the iOS platform. Therefore, when you allocate memory
33、 for an object that you create, you are responsible for releasing that memory when you are finished with it. If you fail to properly free the memory that you allocate, the total amount of memory consumed by your application will grow as the application runs. This failure to clean up unused memory re
34、sults in a memory leak. Eventually, if your program consumes too much memory, the OS will terminate your application. This may seem difficult and complicated, but as long as you have an understanding of how memory management works in Objective-C and remember a few simple rules, you will be fine. In
35、iOS 5, Apple has introduced a new language feature to Objective-C called Automatic Reference Counting, or ARC. While ARC simplifies reference counting by eliminating most of the work for the devel- oper, it is still important for you to have a basic understanding of how reference counting works in O
36、bjective-C. You will learn more about ARC in the next section. All Objective-C classes that inherit from NSObject maintain a retain count. The retain count is a counter that indicates the number of bits of code that are interested in the object. When an object is allocated by using alloc or new, its
37、 retain count is set to 1. In Figure 4-3, your object is represented by the blue cube, and the retain count is in the pink circle. When you are finished using the object, you should call the release method. Calling release reduces the retain count of the object by one. When the retain count reaches
38、zero, the object is de-allocated and the Objective-C runtime calls its dealloc method. You can see this illustrated in Figure 4-3 (a). The gray cube represents the de-allocated object. In addition to release, you can call the autorelease method. This indicates that the runtime should release the obj
39、ect at a point in the future, but not right away. Effectively, this reduces the retain count by 1 just like calling release. You can see this illustrated in Figure 4-3 (b). FIGURE 4-3: Memory managementYou use autorelease pools to keep track of all autoreleased objects. There is an application-wide
40、autorelease pool that most project templates create automatically when you begin building your application. You can create local pools yourself as well. You will see the autorelease pool in action in the game example at the end of this chapter. Autorelease is particularly useful for returning object
41、s from methods. You can allocate the object that you plan to return from the method, configure it, and then autorelease it before returning it to the caller. It is the callers responsibility to retain the object. The Objective-C runtime will send an autoreleased object the release message one time f
42、or every time you call autorelease on it when the autorelease pool is drained or de-allocated. When you need to hold on to a reference to an object, like an autoreleased object that you got back from a method call, you increment the retain count by calling the retain method on the object. Figure 4-3
43、 (c) illustrates this situation. It is the responsibility of the caller of a method that returns an object to call retain. The default autorelease pool will de-allocate autoreleased objects eachtime through the applications run loop. So, if you get an autoreleased object from a function, use it righ
44、t away, and you do not need it after the method call is complete, you do not need to worry about retaining it. However, if you plan to put an autoreleased object into an instance variable for access at a later time, you have to call retain on it or else it will be de-allocated at the end of the run
45、loop and your application will crash when you try to send a message to the de-allocated object. You should use release instead of autorelease whenever possible as there is less overhead in calling release. If you are going to be creating and autoreleasing many objects, in a loop perhaps, you should
46、wrap the loop in its own autorelease pool. You do this by creating an instance of the NSAutoreleasePool object before you start looping. Then, you drain the autorelease pool by releasing it: NSAutoreleasePool *pool = NSAutoreleasePool alloc init; loop a bunch of times pool release; The key thing to
47、remember about memory management in Objective-C is that you need to balance calls to new, alloc, or retain with calls to release or autorelease. If you have too few calls to release/autorelease, the retain count for the object will never drop to 0 and the object will never be de-allocated, resulting
48、 in a memory leak. If you call release or autorelease too many times, you will over-release the object, causing an application crash. In general, adding an object to a Cocoa collection such as NSMutableArray, increments the retain count. Likewise, removing an object from a collection decrements the
49、retain count. Simply obtaining an object from a collection typically returns an autoreleased object. If you need to hold on to the reference to an autoreleased object, you need to call retain on it. You will learn more about working with Cocoa collection classes in the next chapter. When you create
50、an object by calling a class helper method that has alloc, new, or copy in its name, it is your responsibility to release that object. Objects created in this way, by convention, have a retain count of 1. If you get an instance of an object using a class helper method that returns an object that doe
51、s not have alloc, new, or copy in its name such as stringWithString, you should assume that the object is autoreleased. You can send the retainCount message to any NSObject to obtain the current retain count of that object. You generally will not use this method in a production application, but it c
52、an be helpful to log the retain count of an object when you are trying to debug memory problems. To summarize: If you create an object with alloc, new, or copy, the object will have a retain count of 1 and you are responsible for calling release. f you get a reference to an object in any other way, you can assume that it has a retain count of 1 and has
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 4.3.2 声音信号采集
- 脑梗塞发热护理方案
- 危险化学品仓库2026年工作总结与2027年工作安排
- 2026年四川省中小学班主任能力大赛评审参考题集(含答案)
- 2026年江苏省水利工程施工安全实操考核试题及评分标准
- 消防救援站2026年度工作总结暨2027年度工作要点
- 监理月报监理规划
- 2026年消防安全设施操作与维护员实操技能测试模拟试题及答案
- 2026年吉林省社区矫正工作业务考试题库(含答案解析)
- 产业技术研究院2026年度工作总结暨2027年度工作要点
- 9.2奉献社会我践行教学 设计 2026-2027学年统编版道德与法治八年级上册
- 2026年会计从业非营利组织会计题库及参考答案
- 中远海运集团社招集中笔试试卷汇编2026
- 2026年烟花爆竹经营单位主要负责人考试题库及答案
- 消防泵房工程监理实施细则
- 工业管道焊接作业指导书
- 延长石油集团笔试题真题
- 株洲市炎陵县招聘事业单位工作人员笔试真题2025
- 口腔修复学(中级356)《专业知识》真题及解析(2026年)
- 工厂设备全生命周期质量管控管理体系
- 高二地理期中考试质量分析
评论
0/150
提交评论