0

我是 Objective CI 的新手,我正在尝试一些示例程序。我不明白 self 和 super 方法在 Objective C 中是如何工作的。在下面的 pgm CashTransaction.m [super trackSpending:amount] 被调用并在 CreditCardTransaction.m [self trackSpending:amount] 被调用。我找不到 self 和 super 之间的区别。super 用于调用基类覆盖方法。self 用于调用子类覆盖方法。这是我的理解。如果有请纠正我我错了。谢谢。

主文件

#import <Foundation/Foundation.h>
#import "BudgetObject.h"
#import "Transaction.h"
#import "CashTransaction.h"
#import "CreditCardTransaction.h"

int main (int argc, const char * argv[]) {

        //!---Creating An Object And Allocating It With Values---
        Budget* budget = [Budget new];
        [budget createBudget:1000.00 withExchangeRate:1.2500];

        //!---Declaring And Adding Elements To An Array---
        NSMutableArray* transactions = [[NSMutableArray alloc] initWithCapacity:10];
        Transaction* aTransaction; 
        aTransaction = [Transaction new];
        [transactions addObject:aTransaction];

        //!---Calculating The No Of Elements In An Array---
        int k;
        k=[transactions count];
        NSLog(@"The count value is:%d",k);

        //!---Selecting According To The Type Of Transaction---
    for(Transaction *iTransaction in transactions){
         switch ([aTransaction returnType]) {
                case cash:
                        [budget spendDollars:[iTransaction returnAmount]];
                        break;
                case credit:
                        [budget changeForeignCurrency:[iTransaction returnAmount]];
                        break;
                default:
                        break;
         }
        }

        Budget* europeBudget = [Budget new];
        [europeBudget createBudget:1000.00 withExchangeRate:1.2500];

        Budget* englandBudget = [Budget new];
        [englandBudget createBudget:2000.00 withExchangeRate:1.5000];

        NSMutableArray* transactions = [[NSMutableArray alloc] initWithCapacity:10];
        Transaction* aTransaction;
        for(int n=1;n<2;n++){
                aTransaction = [CashTransaction new];
                [aTransaction createTransaction:n*100 forBudget:europeBudget];
                [transactions addObject:aTransaction];

                aTransaction = [CashTransaction new];
                [aTransaction createTransaction:n*100 forBudget:englandBudget];
                [transactions addObject:aTransaction];  
        }
        int n=1;
        while (n<4) {
                aTransaction = [CreditCardTransaction new];
                [aTransaction createTransaction:n*100 forBudget:europeBudget];
                [transactions addObject:aTransaction];

                aTransaction = [CreditCardTransaction new];
                [aTransaction createTransaction:n*100 forBudget:englandBudget];
                [transactions addObject:aTransaction];

                n++;    
        }
        for(Transaction* aTransaction in transactions){
                [aTransaction spend];
        }
return 0;
}

预算对象.h

#import <Cocoa/Cocoa.h>
@interface Budget : NSObject {
        float exchangeRate;
        double budget;
        double exchangeTransaction;
}
- (void) createBudget: (double) aBudget withExchangeRate: (float) anExchangeRate;
- (void) spendDollars: (double) dollars;
- (void) changeForeignCurrency: (double) foreignCurrency;
@end

预算对象.m

#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

事务.h

#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

事务.m

#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

现金交易.h

#import <Cocoa/Cocoa.h>
#import "Transaction.h"
@interface CashTransaction : Transaction {

}
@end

现金交易.m

#import "CashTransaction.h"
#import "BudgetObject.h"
@implementation CashTransaction
- (void) spend{
        [super trackSpending:amount];
        [budget spendDollars:amount];
}
@end

CreditCardTransaction.h

#import <Cocoa/Cocoa.h>
#import "Transaction.h"
@interface CreditCardTransaction : Transaction {

}
@end

信用卡交易.m

#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
4

3 回答 3

15

self并且super工作方式完全不同:表示运行时self的调用对象,而表示方法定义所在类的超类。在这两种情况下,他们都指定了方法的搜索应该从哪里开始,如果起点是动态确定的,如果它在编译时是已知的。superselfsuper

这是一个虚构的例子:

@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

所以我们有三个班级;Grandparent,ParentChild; 各有一个方法One。二类Parent has a method一自超` which calls。运行它会产生:onand

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情况,调用调用从其继承[c Two]的方法-Two所以我们有继承。ChildParent

现在 asTwo执行它首先调用[self One]并且self是 的一个实例Child,它有一个One,所以Child'sOne被执行——这是基于继承的多态性;在定义ParentTwo未来存在Child是未知的,但在执行时调用[self One]能够调用Child的方法。

下一个调用Two[super One]。现在已知这在定义时指的是Grandparent's One

通常super不是指超类中的方法(就像在本例中那样),而是指类型为超类的对象将调用的方法,例如,它可能属于Greatgrandparent. 然而,无论调用什么方法都可以在编译时确定,因为任何类的祖先都是已知的。

调用[self *method*]甚至[super *method*]可以调用相同的方法,在前一种情况下是动态发现的,在后一种情况下是静态已知的。

希望您现在可以将继承应用selfsuper您的示例中。

于 2011-04-15T11:18:26.740 回答
3

self 是指在 Objective-C 编程中接收消息的对象。

在自身上调用方法以通常的方式搜索该方法的方法实现,从接收对象类的调度表开始。

示例:[self startThread]; self.hostReach = YES; 布尔值 = self.hostReach;

1.self 也是一个变量名,可以以多种方式使用,甚至可以分配一个新值。2.实例方法内部,self指的是实例;但在类方法内部,self 指的是类对象。

super 是一个标志,它告诉编译器在一个非常不同的地方搜索方法实现。它从定义 super 出现的方法的类的超类开始。

无论 super 接收到什么消息,编译器都会用另一个消息传递例程代替 objc_msgSend 函数。替代例程直接查看定义类的超类,即查看发送消息给 super 的类的超类,而不是查看接收消息的对象的类。消息到 super 允许方法实现分布在不止一堂课。 

对于某些任务,继承层次结构中的每个类都可以实现一个方法,该方法完成部分工作并将消息传递给 super 以完成其余工作。初始化一个新分配的实例的 init 方法就是这样设计的。每个 init 方法都负责初始化其类中定义的实例变量。但在这样做之前,它会向 super 发送一条 init 消息,以使其继承的类初始化它们的实例变量。每个版本的 init 都遵循这个过程,因此类按照继承顺序初始化它们的实例变量。

http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectivec/Chapters/ocDefiningClasses.html

于 2012-08-02T15:23:51.633 回答
1

超级唤起覆盖方法。事情是,你的超级方法是空的。

如果我理解你的想法是正确的,那么你想创建 CashTransactions 和 CCTransactions,并使用 Transaction 类与他们交谈。这样,每次调用 Transaction 花费方法都会触发正确的子方法。那就是多态性。您只需在运行时处理母类。孩子不需要超级,除非你留下一些所有孩子共有的代码。

于 2011-04-15T09:13:01.857 回答