定义类3

本文详细介绍了Objective-C中self和super的概念及其工作原理。self代表当前对象本身,而super用于指向当前类的父类。通过实例展示了两者在方法调用过程中的不同行为,特别是在多级继承结构中的应用。

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

Messages to self and super

Objective-C provides two terms that can be used within a method definition to refer to the object that performs  the method—self andsuper.

Object C 提供self 跟 super 使得在方法内部可以调用自己的方法

Suppose, for example, that you define a reposition method that needs to change the coordinates of whatever object it acts on. It can invoke thesetOrigin::method to make the change. All it needs to do is send a setOrigin::message to the same object that thereposition message itself was sent to. When you’rewriting the reposition code, you can refer to that object as eitherselforsuper.Therepositionmethodcould read either:

- reposition
{
    ...
    [self setOrigin:someX :someY]; 不能像c那样直接就setOrigin,,,,()
    ...

}


or:

- reposition
{
    ...
    [super setOrigin:someX :someY];
    ...

}

self 跟super的工作机制是不一样的,self 接受到消息是从自身的dispatch table 开始查找,而super则是这个类的超类,,需要继续翻译。。。。。

Here,selfandsuperboth refer to the object receiving arepositionmessage, whatever object that mayhappen to be. The two terms are quite different, however.selfis one of the hidden parameters that themessaging routine passes to every method; it’s a local variable that can be used freely within a methodimplementation, just as the names of instance variables can be.superis a term that substitutes forselfonlyas the receiver in a message expression. As receivers, the two terms differ principally in how they affect themessaging process:

selfsearches for the method implementation in the usual manner, starting in thedispatch tableof the  receiving object’s class. In the example above, it would begin with the class of the object receiving thereposition message.

superis a flag that tells the compiler to search for the method implementation in a very different place.It begins in the superclass of the class that defines the method wheresuperappears. In the exampleabove, it would begin with the superclass of the class where reposition is defined.

Whereversuperreceives a message, the compiler substitutes another messaging routine for theobjc_msgSendfunction. The substitute routine looks directly to the superclass of the defining class—that is, to the superclassof the class sending the message tosuper—ratherthan to the class of the object receiving the message.


An Example: Using self and super

The difference between self and super becomes clear when using a hierarchy of three classes. Suppose, forexample, that we create an object belonging to a class called Low. The superclass of Low is Mid; the superclassof Mid is High. All three classes define a method called negotiate, which each class uses for its own purpose.In addition, Mid defines an ambitious method called makeLastingPeace, which itself employs the negotiatemethod. The classes and those methods are illustrated in Figure 2-2.

Figure 2-2 The hierarchy of High, Mid, and Low

uppose that the implementation of makeLastingPeace (in the Mid class) uses self to indicate the objectto send the negotiate message to:

superclass– negotiate

superclass

– negotiate – makeLastingPeace

superclass– negotiate

- makeLastingPeace
{
    [self negotiate];
    ...

}


When a message is sent to a Low object to perform the makeLastingPeace method, makeLastingPeacesends a negotiate message

to the same Low object. The messaging routine finds the version of negotiatedefined in Low, the class of self.

However, if the implementation of makeLastingPeace instead uses super as the receiver,

- makeLastingPeace
{
    [super negotiate];

...}

the messaging routine finds the version of negotiate defined in High. It ignores the class (Low) of the object that received the

makeLastingPeace message and skips to the superclass of Mid, because Mid is where makeLastingPeace is defined.

Neither implementation finds the Mid version of negotiate.


As this example illustrates, super provides a way to bypass a method that overrides another method. Here,the use of super

enabled makeLastingPeace to bypass the Mid version of negotiate that redefined the High version of that method.

Not being able to reach the Mid version of negotiate, as just described, may seem like a flaw[瑕疵], but under thecircumstances it’s

intentional:

The author of the Low class intentionally overrode the Mid version of negotiate so that instances of Low(and its subclasses) would

invoke the redefined version of the method instead. The designer of Low didn’twant Low objects to perform the inherited method.

The author of the Mid method makeLastingPeace, in sending the negotiate message to super (asshown in the second implementation), intentionally skipped over the Mid version of negotiate (andover any versions that might be defined in classes like Low that inherit from Mid) to perform the versiondefined in the High class. The designer of the second implementation of makeLastingPeace wantedto use the High version of negotiate and no other.

The Mid version of negotiate could still be used, but it would take a direct message to a Mid instance to doso.


简单的说,self从自身声明的方法开始查找,如果没有会转到超类查找,如果是super则直接就从父类开始查找了,即使本类的声明有也不会查找。






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值