A method has automatic access to the receiving object’s instance variables. You don’t need to pass them to
the method as parameters. 方法可以直接访问到对象的成语变量
A method has automatic access only to the receiver’s instance variables. If it requires information about a variable stored in another object, it must send a message to the object asking it to reveal the contents of the variable. 方法要拿其他对象的instance variables 需要向其他对象发送message.
As the earlier examples illustrate, messages in Objective-C appear in the same syntactic positions as function calls in standard C. But, because methods “belong to” an object, messages don’t behave in the same way thatfunction calls do.
虽然消息的调用跟标准C函数调用在语法上处于相同的位置,但由于方法属于对象,故消息调用跟函数的调用行为是不一样的.
In particular, an object can be operated on by only those methods that were defined for it. It can’t confuse them with methods defined for other kinds of object, even if another object has a method with the same name. Therefore, two objects can respond differently to the same message. For example, each kind of object that receives a display message could display itself in a unique way. A Circle and a Rectangle would respond differently to identical instructions to track the cursor.
每个对象都只相应自己的消息,,相同的消息发给不同的对象是不一样的.
这中特性称为多态,,与动态绑定一起,可以让代码在运行时候决定调用拿一个对象,而不需要编码写死.
They might even be objects that will be developed later, by other programmers working on other projects. If you write code that sends a display message to an id variable, any object that has a display method is a potential receiver.
Dynamic Binding
A crucial difference between function calls and messages is that a function and its parameters are joined together in the compiled code, but a message and a receiving object aren’t united until the program is running and the message is sent. Therefore, the
exact method invoked to respond to a message can be determined
only at runtime, not when the code is compiled.
消息调用跟函数调用的关键不同地方是,函数跟他的参数是编译时已经组合在一起的.而消息的调用则在运行时候,由对象去调用函数从而响应相应的消息.
When a message is sent, a runtime messaging routine looks at the receiver and at the method named in the message. It locates the receiver’s implementation of a method matching the name, “calls” the method, and passes it a pointer to the receiver’s instance
variables. (For more on this routine, see “Messaging” in Objective-C
Runtime Programming Guide .)
当消息发送时候,运行时消息例程查看接收者跟方法名称,从而定位接收者,并调用其方法,并传递指针给接收者的实例变量.
This dynamic binding of methods to messages works hand in hand with polymorphism to give object-oriented programming much of its flexibility and power. Because each object can have its own version of a method, an Objective-C statement can achieve a variety
of results, not by varying the message but by varying the object
that receives the message. Receivers can be decided as the program runs; the choice of receiver can be made dependent on factors such as user actions.
Objective-C takes dynamic binding one step further and allows even the message that’s sent (the method selector) to be a variable determined at runtime. This mechanism is discussed in the section “Messaging” in Objective-C Runtime Programming Guide .
Object c 动态绑定 还可以发送消息给变量
Dot Syntax (消息调用的另一选择)
Objective-C provides a dot (.) operator that offers an alternative to square bracket notation ([]) to invoke accessor methods. Dot syntax uses the same pattern that accessing C structure elements uses:
myInstance.value = 10;
printf("myInstance value: %d", myInstance.value);
When used with objects, however, dot syntax acts as “syntactic sugar”—it is transformed by the compiler into an invocation of an accessor method. Dot syntax does not directly get or set an instance variable. The code example above is exactly equivalent to the following:
等同于:
[myInstance setValue:10];
printf("myInstance value: %d", [myInstance value]);
As a corollary, if you want to access an object’s own instance variable using accessor methods, you must
explicitly call out self, for example:
self.age = 10; //访问实例变量
or the equivalent:
[self setAge:10]; //通过消息
If you do not use self., you access the instance variable directly. In the following example, the set accessor
method for age is not invoked:
age = 10; //not invoke
If a nil value is encountered during property traversal, the result is the same as sending the equivalent message to nil. For example, the following pairs are all equivalent:
// Each member of the path is an object.
x = person.address.street.name;
x = [[[person address] street] name];
// The path contains a C struct.
// This will crash if window is nil or -contentView returns nil.
y = window.contentView.bounds.origin.y;
y = [[window contentView] bounds].origin.y;
// An example of using a setter.
person.address.street.name = @"Oxford Road";
[[[person address] street] setName: @"Oxford Road"];