首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSCopying和继承

NSCopying和继承
EN

Stack Overflow用户
提问于 2010-08-06 19:36:21
回答 2查看 2.9K关注 0票数 5

请看下面的代码:- .h

代码语言:javascript
复制
@interface BankAccount : NSObject<NSCopying>
{
    double accountBalance;
    long accountNumber;
    NSString *CustomerName;
    NSString *AccountType;
}

-(void) setAccount: (long) y andBalance: (double) x;
-(void) setCustomerName: (NSString*) name andAccountType: (NSString*) type;
-(id)copyWithZone:(NSZone *)zone;

@end

@interface Savings : BankAccount
{
    int number;
    NSString *Offer;
}
-(void) setSavingNumber: (uint8_t) num andOffer: (NSString*) offer;
-(id)copyWithZone:(NSZone *)zone;
@end

- .m

代码语言:javascript
复制
@implementation BankAccount

-(void) setAccount: (long) y andBalance: (double) x
{
    accountNumber = y;
    accountBalance = x;
}
-(void) setCustomerName: (NSString*) name andAccountType: (NSString*) type
{
    CustomerName = name;
    AccountType = type;
}

-(id)copyWithZone:(NSZone *)zone
{
    BankAccount *accountCopy = [[BankAccount allocWithZone: zone] init];
    [accountCopy setAccount: accountNumber andBalance: accountBalance];
    [accountCopy setCustomerName:CustomerName andAccountType:AccountType];
    return accountCopy;
}

@end

@implementation Savings
-(void) setSavingNumber: (uint8_t) num andOffer: (NSString*) offer
{
    number = num;
    Offer = offer;
}

-(id)copyWithZone:(NSZone *)zone
{
    Savings * clone = [super copyWithZone:zone];
    [clone setSavingNumber:number andOffer:Offer];************** error *********
    return clone;
}

@end

运行此代码时:

代码语言:javascript
复制
Savings* account1;
Savings* account2;

account1 = [[Savings alloc] init];
[account1 setAccount:10 andBalance:1000.10];
[account1 setCustomerName:[NSString stringWithFormat:@"%@",@"Deepak"] andAccountType:[NSString stringWithFormat:@"%@",@"Savings"]];
[account1 setSavingNumber:2001 andOffer:@"Bad"];    
account2 = [account1 copy];

我不知道代码有什么问题,请帮帮我。提前谢谢。

谢谢Deepak

EN

回答 2

Stack Overflow用户

发布于 2011-05-27 02:52:31

您的基本copyWithZone:方法应如下所示:

代码语言:javascript
复制
-(id)copyWithZone:(NSZone *)zone {
    // change this line to use [self class]
    BaseClass *base = [[[self class] allocWithZone:zone] init];

    // copy base members:
    // ...

    return base;
}

派生的copyWithZone:方法应该如下所示:

代码语言:javascript
复制
-(id)copyWithZone:(NSZone *)zone {
    DerivedClass *derived = [super copyWithZone:zone];

    // copy derived members:
    // ...

    return derived;
}

还要确保你正在制作强引用的深层副本和弱引用的浅副本。因此,例如,要复制类型为NSStringNSArray的成员(每个成员都具有强引用成员,一个成员具有弱成员),请执行以下操作:

代码语言:javascript
复制
derived.strongString    = [[strongString copyWithZone:zone] autorelease];
derived.weakString      = weakString;
derived.strArrWStrVals  = [[strArrWStrVals copyWithZone:zone] autorelease];
derived.strArrWWeakVals = [[[NSArray allocWithZone:zone]
                            initWithArray:strArrWWeakVals] autorelease];
derived.weakArray       = weakArray;

(通常还会分配/保留弱成员,并复制强变量。)

请注意,由于这个原因,您不应该使用initWithMyself:类型方法来复制数据。

票数 30
EN

Stack Overflow用户

发布于 2010-08-07 11:20:02

好的,首先,您的测试代码是错误的,因为您在一个8位整数中设置了保存数字2001。其次,您的代码不会运行,因为您试图在BankAccount而不是Saving对象上调用setSavingNumber:andOffer:,这样它就无法在运行时找到此方法的选择器。感谢David指出这一点。

在实现BankAccount::copyWithZone时,您使用了alloc-init,并且返回的对象没有问题。在实现Savings::copyWithZone时,调用超级copyWithZone。你得到的是一个BankAccount类型的对象,所以你不能对它使用setSavingNumber:和of:。由于您在基类中使用了alloc-init,因此还应该在Savings类中使用alloc-init和setMethods。

为了避免重复代码,我会重新注释在BankAccount中实现一个initWithBankAccount,在Savings中也是如此。

然后在copyWithZone中,你会有

代码语言:javascript
复制
return [[BankAccount allocWithZone:zone] initWithBankAccount:self];

代码语言:javascript
复制
return [[Savings allocWithZone:zone] initWithSavings:self];

尽管您必须确保在initWithSavings中调用

代码语言:javascript
复制
self = [super initWithBankAccount:savings];

或者直接调用init并复制基成员初始化。

看看内存管理编程指南中的Implementing object copy

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3423366

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档