﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-Oriental Design Space-文章分类-Objective C翻译</title><link>http://www.cppblog.com/alzhang/category/18574.html</link><description>using C/Cpp/ObjC</description><language>zh-cn</language><lastBuildDate>Tue, 14 Feb 2012 00:45:54 GMT</lastBuildDate><pubDate>Tue, 14 Feb 2012 00:45:54 GMT</pubDate><ttl>60</ttl><item><title>第一章 Objects, Classes, and Messaging</title><link>http://www.cppblog.com/alzhang/articles/165249.html</link><dc:creator>alzhang</dc:creator><author>alzhang</author><pubDate>Thu, 09 Feb 2012 15:11:00 GMT</pubDate><guid>http://www.cppblog.com/alzhang/articles/165249.html</guid><wfw:comment>http://www.cppblog.com/alzhang/comments/165249.html</wfw:comment><comments>http://www.cppblog.com/alzhang/articles/165249.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/alzhang/comments/commentRss/165249.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/alzhang/services/trackbacks/165249.html</trackback:ping><description><![CDATA[<div>  <h1>Objects, Classes, and Messaging</h1>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">这章描述了</span>ObjC<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">语言所使用和实现的对象，类，和消息机制的基本。同时介绍了</span>ObjC<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">的动态运行时。</span></p>  <h2>The Running System</h2>  <p>ObjC<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">语言之所以慢是因为从编译、链接时间到运行时有着许多的决定需要做。只要有可能，编译器会自动地增加许多操作：诸如创建对象，决定调用哪个方法（</span>method<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">）。因此，这个语言不仅仅需要的是一个编译器，而是一个运行时系统能够执行编译的代码。这个运行时系统扮演着</span>ObjC<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">语言的操作系统，它使得这个语言能够工作。然而，一般来讲我们并不需要与运行时直接打交道。为了更多地了解运行时所提供的功能，不过可以参见</span><em><span>Objective-C Runtime Programming Guide</span></em><em><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">。</span></em></p>  <h2>Objects</h2>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">如题所述，面向对象编程总是围绕着对象（</span>objects<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">）而建立的。对象关联着数据和特殊的操作，它们能够使用或改变这些数据。</span>ObjC<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">语言提供了一种数据类型能够识别一个对象变量而无需指定特定的对象的类。</span></p>  <h3>Object Basics</h3>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">对象关联着数据和特殊的操作，它们能够使用或改变这些数据。在</span>ObjC<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">中，这些操作被称为对象的方法；它们所改变的数据被称为实例变量（</span>instance variable<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">）（在其他的环境中，它们被称为</span>ivars<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">或成员变量）。从根本上来说，一个对象绑定着一个数据结构（实例变量）和一组程序（方法）到一个自我封闭的编程单元中。</span></p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">在</span>ObjC<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">中，对象的实例变量时对象的内部；通常而言，你只能通过对象的方法获取对象的状态（你可以通过范围指示（</span>scope directive<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">）指定子类或其他对象可以获得的实例变量，参见</span>The Scope of Instance Variables Page40<span style="font-family: 宋体;Times New Roman&quot;;Times New Roman&quot;">）。对于其他的需要找到与对象相关的东西，则必须有一个对象能够提供相关的信息。例如，长方形需要有方法显示他的尺寸和位置。</span></p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">再多说点，一个对象只能看到为他设计的方法；它不可能错误地使用为其他的类型的对象而设计的方法。正如</span>C<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">函数一样会保护自己局部变量，使得他们对程序的其余部分是隐藏的。一个对象隐藏自己的实例变量和方法的实现。</span></p>  <h3>id</h3>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">在</span>ObjC<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">中，对象标识符时一个独特的数据类型：</span>id<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">。这个内型时任何对象的通用内型，无论何种类。可以用作一个类的实例和类对象自身。</span></p>  <p style="text-indent:15.75pt;">id anObject;</p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">对于由</span>ObjC<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">构建起来的面向对象，如方法的返回值，</span>id<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">代替了</span>int<span style="font-family: 宋体;Times New Roman&quot;;Times New Roman&quot;">作为默认的数据类型。（对于严格的</span>C<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">构建，比如函数的返回值，</span>int<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">仍然是默认的类型。）</span></p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">关键词</span>nil<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">被定义成空对象，拥有</span>id<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">为</span>0.id<span style="font-family: 宋体;Times New Roman&quot;;Times New Roman&quot;">和</span>nil<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">以及其他的</span>ObjC<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">的基本数据类型被定义在头文件</span>objc/objc.h<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">中。</span></p>  <p>Id<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">被定义成指针指向对象的数据结构：</span></p>  <p>typedef struct objc_object {</p>  <p><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Class isa;</span></p>  <p>} *id;</p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">这样，每个对象都拥有一个</span>isa<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">变量，可以告诉我们它是哪个类的实例。因为</span>Class<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">类型本身就定义成了一个指针：</span></p>  <p>typedef struct objc_class *Class;</p>  <p>isa<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">变量通常称为</span>isa<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">指针。</span></p>  <h3>Dynamic Typing</h3>  <p>Id<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">类型是完全非限制的。就其自身而言，它不产生关于对象的任何信息，除了它是一个对象。在某些时候，一个程序特别需要找到对象所含的特别信息。因此，</span>id<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">类型设计师不能提供特别的信息给编译器，那么每个对象需要在运行时提供它们。</span></p>  <p>Isa<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">实例变量能够标示对象的类</span>---<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">表明他是什么类型的对象。有着相同行为（</span>method<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">）和相同数据（实例变量）的对象是同一个类的成员。</span></p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">因此，对象是在运行时动态指定的。当有必要时，运行时系统可以找到所属的对象，只要通过问对象（想了解更多，参见</span><em><span>Objective-C Runtiming Programming Guide</span></em><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">）</span>ObjC<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">中的动态类型是动态绑定的基础，在稍后的章节我们继续讨论。</span></p>  <p>Isa<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">变量可以使对象进行自省（即，对象可以通过自身的</span>isa<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">变量可以自己属于哪个类）</span><span>--- </span><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">能够找出自己的信息（或者其他的对象。）编译器会在数据结构中记录有关类定义的信息，供运行时系统使用。运行时系统函数使用</span>isa<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">找到有关运行时的信息。例如，使用运行时系统，你可以决定一个对象是否实现了一个特殊的方法或者找到父类的名字。</span></p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">对象类在</span>Class<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">（第</span>19<span style="font-family: 宋体;Times New Roman&quot;;Times New Roman&quot;">页）章节有更详细的讨论。</span></p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">也有可能要给编译器有关对象类的静态类型，这种类型是通过类名在源文件中指定的。类是对象的特殊类型，类名可以用作类型名。参见</span><em>Class Types</em><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">（第</span>23<span style="font-family: 宋体;Times New Roman&quot;;Times New Roman&quot;">页）</span> <span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">和</span><em>Enabling Static Behavior</em> (<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">第</span>83<span style="font-family: 宋体;Times New Roman&quot;;Times New Roman&quot;">页</span>)<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">。</span></p>  <h3>Memory Management </h3>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">在任何程序中，当对象不再需要时，确保它能够被销毁时非常重要的</span>---<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">否则你的应用程序的内存的</span>footprint<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">就会变得非常大。同时，当对象正在使用时，确保对象不能够被删除。</span></p>  <p>ObjC<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">提供了三种机制使得内存管理可以达到以上的目标</span></p>  <p style="margin-left:21.0pt;text-indent:-21.0pt;"><span style="font-family: Wingdings;"><span>l<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">自动引用计数（</span>Automatic Reference Counting<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">）：其中编译器可以推断对象的使用期。</span></p>  <p style="margin-left:21.0pt;text-indent:-21.0pt;"><span style="font-family: Wingdings;"><span>l<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">手动引用计数（</span>Manual Reference Counting<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">）：其中你可以完全自主地决定对象的使用期。</span></p>  <p style="margin-left:21.0pt;text-indent:-21.0pt;"><span style="font-family: Wingdings;"><span>l<span style="font:7.0pt &quot;Times New Roman&quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">垃圾收集（</span>Garbage collection<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">）：其中你可以把决定对象使用期的职责转交给自动收集器。</span></p>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">垃圾收集在</span><em>Garbage Collection Programming Guide</em><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">中有表述（</span>iOS<span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">不可用）</span></p>  <h2>Object Message</h2>  <p><span style="font-family:宋体;Times New Roman&quot;;Times New Roman&quot;">本节解释了发送消息的语法，包括</span></p>  </div><img src ="http://www.cppblog.com/alzhang/aggbug/165249.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/alzhang/" target="_blank">alzhang</a> 2012-02-09 23:11 <a href="http://www.cppblog.com/alzhang/articles/165249.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>