object-c 学习第三天(方法和消息)

从今天开始我还是以翻译为主,以后慢慢理解

官方原文地址:

https://developer.apple.com/library/mac/#referencelibrary/GettingStarted/RoadMapOSX/books/WriteObjective-CCode/WriteObjective-CCode/WriteObjective-CCode.html

方法和消息

object-c中有2中方法:实例方法和类方法

一个实例方法是作用于一些特殊类实例的方法,换句话说,在你调用一个实例方法之前。你必须先创建类的实例,实例方法是最通用的方法。

类方法不需要对象的实例作为消息的接受者

There are two kinds of methods in Objective-C: instance methods and class methods.

·  Aninstance method is a method whose execution is scoped to a particular instance of the class.In other words, before you call an instance method, you must first create an instance of the class. Instance methods are the most common type of method.

·  Aclass method is a method whose execution is scoped to the method’s class. It does not require an instance of an object to be the receiver of a message.


对于实例方法在方法前面用“-”,对于类方法在方法前面用“+”,接下来我们将重点详细的介绍类对象

For instance methods, the declaration is preceded by a minus sign (-); for classmethods, the corresponding indicator is a plus sign (+). “ClassMethods”, below, describes class methods in greater detail.


一个方法的实际名字是和其他的一些关键字串联在一起的,(包括冒号),冒号后面声明了参数,在下面这个例子当中,这个方法有两个参数,如果一个方法没有参数的话,你可以省略冒号

-(void) print;

A method’s actual name (insertObject:atIndex:) is aconcatenation of all of the signature keywords, including colon characters. Thecolon characters declare the presence of a parameter. In the above example, themethod takes two parameters. If a method has no parameters, you omit the colonafter the first (and only) signature keyword.


当你想要调用方法,你可以传递一个消息给这个对象来实现——换句话说就是通知那个对象,一个消息是一个方法的名字连同方法需要的参数名字(当然得要合适的类型),所有你发送给对象的参数都会被动态的分配,这样可以促进类的多态(多态指的是不同类型的对象回应同一个消息)有时一个方法的调用被一个类的超类(父类)的对象来接受消息。

When you want to call a method, you do so by sending a message to theobject that implements the method—or, in other words, bymessaging thatobject. (Although the phrase "sending a message" is commonly used asa synonym for "calling a method,“ the Objective-C runtime does the actualsending.) A message is the method name along with the parameter information themethod needs (properly conforming to type). All messages you send to an objectare dispatched dynamically, thus facilitating the polymorphic behavior ofObjective-C classes. (Polymorphism refers to the ability of different types ofobjects to respond to the same message.) Sometimes the method invoked isimplemented by a superclass of the class of the object receiving the message.


为了发送一个消息,运行需要一个消息表达式,每个消息表达式放在方括”[ ]”号里(包括参数和消息本身),为了发送一个消息,例如:要发送一个insertObject:atIndex的消息给一个由myArray变量保存的对象,你可以用如下的语法:

To dispatch a message, the runtime requires a message expression. Amessage expression encloseswith brackets ([ and]) the message itself (along with any required parameters) and, just insidethe leftmost bracket, the object receiving the message. For example, to sendtheinsertObject:atIndex: message toan object held by themyArray variable, you would use the following syntax:

[myArray insertObject:anObject atIndex:0];


为了避免声明很多本地变量来存储临时结果,object-c可以让你筑造消息表达式,每个表达式的返回值用来作为一个参数或者用来接收对象的另一个消息。例如,你可以用消息来替代任何之前例子中的变量,这样可以重新得到变量的值

比如:你有另外一个对象叫做myAppObject方法可以访问数据对象,并且可以把这个对象插入到数组中,你就可以写如下的表达式

To avoid declaring numerous local variables to store temporary results,Objective-C lets you nest message expressions. The return value from eachnested expression is used as a parameter or as the receiving object of anothermessage. For example, you could replace any of the variables used in theprevious example with messages to retrieve the values. Thus, if you had anotherobject calledmyAppObject that had methods for accessing the array object andthe object to insert into the array, you could write the preceding example tolook something like the following:

[[myAppObject theArray] insertObject:[myAppObject objectToInsert] atIndex:0];




点符号型语法 object-c也提供“点符号”的语法用来调用存储方法,存储方法设置或者得到对象的状态

方法得到或者设置对象的状态,这些都是封装的关键,也是所有对象的一个重要的特点,对象隐藏或者封装,他们提供一个供所有实例访问他们状态的接口。用点符号语法,你可以把之前的例子改写成如下格式:

methods get and setthe state of an object, and thus are key to encapsulation, which is animportant feature of all objects. Objects hide, or encapsulate, theirstate and present an interface common to all instances for accessing thatstate. Using dot-notation syntax, you could rewrite the previous example as:

[myAppObject.theArray insertObject:myAppObject.objectToInsert atIndex:0];


你也可以使用点符号型语法用来定义

Youcan also use dot-notation syntax for assignment:

myAppObject.theArray = aNewArray;


但是这个语法在写 [myAppObject setTheArray:aNewArray]这句的时候有点不同,对于动态类型你不能使用“点符号”型的。

This syntax is simply a different way to write [myAppObject setTheArray:aNewArray];. You cannot use a reference to a dynamically typed object (type ofid) in a dot-notation expression.


好了,可能是我理解的不够,翻译有误的地方请见谅,我也在不断的学习进步,加油!每天都要坚持



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值