Objective c 目标C:自我与超自我的区别
我不熟悉目标C。我正在尝试一些示例程序。我无法理解self和super方法在目标C中的工作原理。在CashTransaction下面的pgm中,调用了m[super-TrackExpanding:amount],并在CreditCardTransaction.m[self-TrackExpanding:amount]中调用调用。我找不到self和super之间的区别。super用于调用基类重写方法。self用于调用子类重写方法。这是我的理解。如果我错了,请更正。谢谢 main.m 交易记录.h 现金交易 信用卡交易 输出:Objective c 目标C:自我与超自我的区别,objective-c,self,super,Objective C,Self,Super,我不熟悉目标C。我正在尝试一些示例程序。我无法理解self和super方法在目标C中的工作原理。在CashTransaction下面的pgm中,调用了m[super-TrackExpanding:amount],并在CreditCardTransaction.m[self-TrackExpanding:amount]中调用调用。我找不到self和super之间的区别。super用于调用基类重写方法。self用于调用子类重写方法。这是我的理解。如果我错了,请更正。谢谢 main.m 交易记录.h
Super调用覆盖方法。问题是,你的super中的方法是空的
如果我理解您的想法是正确的,那么您需要创建CashTransactions和CCTransactions,并使用Transaction类与他们交谈。这样,每次调用Transaction Expense方法都会触发正确的子方法。这就是多态性。您只需在运行时处理母类。孩子不需要super,除非你给孩子们留下一些共同的代码。
self
和super
的工作方式完全不同:self
表示运行时调用的对象,而super
表示方法定义所在类的超类。在这两种情况下,它们都指定了方法搜索的开始位置,在self
的情况下,起始点是动态确定的,在super
的情况下,它是在编译时已知的
下面是一个虚构的例子:
@interface Grandparent : NSObject
- (void) One;
@end
@implementation Grandparent
- (void) One { NSLog(@"Grandparent One\n"); }
@end
@interface Parent : Grandparent
- (void) One;
- (void) Two;
@end
@implementation Parent
- (void) One { NSLog(@"Parent One\n"); }
- (void) Two
{
[self One]; // will call One based on the calling object
[super One]; // will call One based on the defining object - Parent in this case so will Grandparent's One
}
@end
@interface Child : Parent
- (void) One;
@end
@implementation Child
- (void) One { NSLog(@"Child One\n"); }
@end
@implementation FamilyAppDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
Child *c = [Child new];
[c Two]; // will call the Two inherited from Parent
Parent *p = [Parent new];
[p Two]; // will call Parent's Two
}
@end
所以我们有三节课<代码>祖父母,父母
和孩子
;每个都有一个方法One
。类Parent有一个方法
Two,它在
self和
super>上调用One。运行此命令将产生:
2011-04-15 22:49:05.006 Family[1993:a0f] Child One
2011-04-15 22:49:05.009 Family[1993:a0f] Grandparent One
2011-04-15 22:49:05.009 Family[1993:a0f] Parent One
2011-04-15 22:49:05.010 Family[1993:a0f] Grandparent One
对于Child
情况,调用[ctwo]
调用Child
从其父继承的方法Two
——因此我们有了继承
现在当执行Two
时,它首先调用[self-One]
,而self
是Child
的一个实例,它有一个One
,因此执行Child
的One
,这是基于继承的多态性;在定义父两个时,子的未来存在未知,但在执行时调用[self-One]
能够调用子的方法
Two
中的下一个调用是[super-One]
。现在我们知道这是指定义时祖父母的一个
一般来说,super
不引用超类中的方法(如本例中所述),而是引用类型为超类的对象将调用的方法,例如,它可能属于,比如说,Greatgrandparent
。然而,无论调用什么方法,都可以在编译时确定,因为任何类的起源都是已知的
调用[self*method*]
和[super*method*]
甚至可以调用相同的方法,在前一种情况下是动态的,在后一种情况下是静态的
希望您现在可以将继承、self
和super
应用到您的示例中
self是指在objective-C编程中接收消息的对象
在self上调用一个方法以通常的方式搜索该方法的方法实现,从接收对象类的dispatch表开始
示例:[自启动线程];self.hostReach=是;BOOL值=self.hostReach
1.self也是一个变量名,可以以多种方式使用,甚至可以指定一个新值。
2.在实例方法中,self指的是实例;但是在类方法中,self引用类对象
super是一个标志,它告诉编译器在一个完全不同的位置搜索方法实现。它从定义super出现的方法的类的超类开始
只要super接收到消息,编译器就会用另一个消息传递例程替换objc_msgSend函数。替换例程直接查找定义类的超类,即向super发送消息的类的超类,而不是接收消息的对象的类。super的消息允许方法实现分布在多个类上
对于某些任务,继承层次结构中的每个类都可以实现一个方法,该方法完成部分作业,并将消息传递给super以完成其余任务。初始化一个新分配的实例的init方法就是这样设计的。每个init方法都负责初始化其类中定义的实例变量。但在执行此操作之前,它会向super发送一条init消息,让它从中继承的类初始化它们的实例变量。init的每个版本都遵循这个过程,因此类按照继承的顺序初始化它们的实例变量
非常感谢。我参与了你向我解释的程序。感谢你用一个例子让我了解了自我和超级。“并且在后者中静态地知道。”不,一个super
调用也是如此dynamic@newacct-您是否区分“发现”和“已知”?或者,您是在暗示底层运行时特性,这些特性允许各种各样的操作,并且属于“herebedragons”类别?在标准情况下,超类以及完整的继承层次结构是已知的。如果实现选择使用作为实现细节的搜索来查找super
方法。@CRD:我指的是,例如,如果您使用super
调用一个方法,但未找到该方法,则正常的Objective-C消息将在
#import "BudgetObject.h"
@implementation Budget
- (void) createBudget: (double) aBudget withExchangeRate: (float) anExchangeRate;
{
budget = aBudget;
exchangeRate = anExchangeRate;
}
- (void) spendDollars: (double) dollars
{
budget = budget - dollars;
NSLog(@"Converting %0.2f into U.S Foreign Currency leaves $%0.2f",dollars,budget);
}
- (void) changeForeignCurrency: (double) foreignCurrency
{
exchangeTransaction = foreignCurrency * exchangeRate;
budget = budget - exchangeTransaction;
NSLog(@"Charging %0.2f in Foreign Currency leaves $%0.2f",foreignCurrency,budget);
}
@end
#import <Cocoa/Cocoa.h>
#import "BudgetObject.h"
@class Budget;
@interface Transaction : NSObject {
Budget* budget;
double amount;
}
- (void) createTransaction: (double) theAmount forBudget: (Budget*) aBudget;
- (void) trackSpending: (double) theAmount;
- (void) spend;
@end
#import "Transaction.h"
#import "BudgetObject.h"
@implementation Transaction
- (void) createTransaction: (double) theAmount forBudget: (Budget*) anBudget {
budget = anBudget;
amount = theAmount;
}
- (void) spend {
}
-(void) trackSpending: (double) theAmount {
NSLog(@"You are about to spend another %0.2f",theAmount);
}
@end
#import <Cocoa/Cocoa.h>
#import "Transaction.h"
@interface CashTransaction : Transaction {
}
@end
#import "CashTransaction.h"
#import "BudgetObject.h"
@implementation CashTransaction
- (void) spend{
[super trackSpending:amount];
[budget spendDollars:amount];
}
@end
#import <Cocoa/Cocoa.h>
#import "Transaction.h"
@interface CreditCardTransaction : Transaction {
}
@end
#import "CreditCardTransaction.h"
#import "BudgetObject.h"
@implementation CreditCardTransaction
- (void) spend {
[self trackSpending:amount];
[budget changeForeignCurrency:amount];
}
@end
2011-04-15 11:24:46.112 Bud Obj1[1041:a0f] You are about to spend another 100.00
2011-04-15 11:24:46.114 Bud Obj1[1041:a0f] Converting 100.00 into U.S Foreign Currency leaves $900.00
2011-04-15 11:24:46.115 Bud Obj1[1041:a0f] You are about to spend another 100.00
2011-04-15 11:24:46.115 Bud Obj1[1041:a0f] Converting 100.00 into U.S Foreign Currency leaves $1900.00
2011-04-15 11:24:46.116 Bud Obj1[1041:a0f] You are about to spend another 100.00
2011-04-15 11:24:46.119 Bud Obj1[1041:a0f] Charging 100.00 in Foreign Currency leaves $775.00
2011-04-15 11:24:46.120 Bud Obj1[1041:a0f] You are about to spend another 100.00
2011-04-15 11:24:46.120 Bud Obj1[1041:a0f] Charging 100.00 in Foreign Currency leaves $1750.00
2011-04-15 11:24:46.121 Bud Obj1[1041:a0f] You are about to spend another 200.00
2011-04-15 11:24:46.121 Bud Obj1[1041:a0f] Charging 200.00 in Foreign Currency leaves $525.00
2011-04-15 11:24:46.122 Bud Obj1[1041:a0f] You are about to spend another 200.00
2011-04-15 11:24:46.122 Bud Obj1[1041:a0f] Charging 200.00 in Foreign Currency leaves $1450.00
2011-04-15 11:24:46.123 Bud Obj1[1041:a0f] You are about to spend another 300.00
2011-04-15 11:24:46.123 Bud Obj1[1041:a0f] Charging 300.00 in Foreign Currency leaves $150.00
2011-04-15 11:24:46.124 Bud Obj1[1041:a0f] You are about to spend another 300.00
2011-04-15 11:24:46.125 Bud Obj1[1041:a0f] Charging 300.00 in Foreign Currency leaves $1000.00
@interface Grandparent : NSObject
- (void) One;
@end
@implementation Grandparent
- (void) One { NSLog(@"Grandparent One\n"); }
@end
@interface Parent : Grandparent
- (void) One;
- (void) Two;
@end
@implementation Parent
- (void) One { NSLog(@"Parent One\n"); }
- (void) Two
{
[self One]; // will call One based on the calling object
[super One]; // will call One based on the defining object - Parent in this case so will Grandparent's One
}
@end
@interface Child : Parent
- (void) One;
@end
@implementation Child
- (void) One { NSLog(@"Child One\n"); }
@end
@implementation FamilyAppDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
Child *c = [Child new];
[c Two]; // will call the Two inherited from Parent
Parent *p = [Parent new];
[p Two]; // will call Parent's Two
}
@end
2011-04-15 22:49:05.006 Family[1993:a0f] Child One
2011-04-15 22:49:05.009 Family[1993:a0f] Grandparent One
2011-04-15 22:49:05.009 Family[1993:a0f] Parent One
2011-04-15 22:49:05.010 Family[1993:a0f] Grandparent One