請對下面的代碼來看看:------- .HNSCopying和繼承
@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
@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
當運行此代碼::::::
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];
#
我不知道什麼是錯的代碼,請幫助我。提前致謝。
感謝 迪帕克
請格式化代碼並提出問題。目前的行爲是什麼,預期會是什麼。 – 2010-08-06 12:30:27