从C#到Objective-C的构造函数代码
作者:互联网
我们必须将C#代码转换为Objective-C代码,而我很难解决如何创建一个不带参数的构造函数,而另一个带2个参数的构造函数.
这是我要转换的C#代码:
namespace Account
{
class Program
{
public class Account
{
private double balance;
private int accountNumber;
public Account()
{
balance = 0;
accountNumber = 999;
}
public Account(int accNum, double bal)
{
balance = bal;
accountNumber = accNum;
}
}
}
}
到目前为止,这是我对目标C所不确定的,甚至不确定它是否正确
@interface classname : Account
{
@private double balance;
@private int accountNumber;
@public Account()
}
开放给我任何帮助,丹尼,非常感谢
解决方法:
您只需提供两个初始化程序,它们采用的一般形式为:
@interface MONAccount : NSObject
@private
double balance;
int accountNumber;
}
/* declare default initializer */
- (id)init;
/* declare parameterized initializer */
- (id)initWithAccountNumber:(int)inAccountNumber balance:(int)inBalance;
@end
@implementation MONAccount
- (id)init
{
self = [super init];
/* objc object allocations are zeroed. the default may suffice. */
if (nil != self) {
balance = 0;
accountNumber = 999;
}
return self;
}
- (id)initWithAccountNumber:(int)inAccountNumber balance:(int)inBalance
{
self = [super init];
if (nil != self) {
balance = inBalance;
accountNumber = inAccountNumber;
}
return self;
}
@end
标签:objective-c,constructor,c 来源: https://codeday.me/bug/20191202/2086957.html