gpt4 book ai didi

c# - 从 C# 到 Objective-C 的构造函数代码

转载 作者:搜寻专家 更新时间:2023-10-30 20:11:20 25 4
gpt4 key购买 nike

我们必须将我们的 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;
}
}
}

这就是我目前对 Objective 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

关于c# - 从 C# 到 Objective-C 的构造函数代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7695079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com