对象,类,消息 2

本文深入探讨了Objective-C中的消息传递机制,包括方法如何访问对象的实例变量、多态性的实现方式及其灵活性、动态绑定的概念及其对编程的影响,以及消息传递的替代语法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The Receiver’s Instance Variables
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.



Polymorphism

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.

每个对象都只相应自己的消息,,相同的消息发给不同的对象是不一样的.


This feature, referred to as polymorphism, plays a significant role in the design of object-oriented programs. Together with dynamic binding, it permits you to write code that might apply to any number of different kinds of objects, without you having to choose at the time you write the code what kinds of objects they might be.

这中特性称为多态,,与动态绑定一起,可以让代码在运行时候决定调用拿一个对象,而不需要编码写死.


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"];





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值