施工人员从C#到Objective-C的代码
我们必须将C#代码转换为Objective-C代码,我很难弄清楚如何创建一个没有参数的构造函数,而另一个有两个参数。 这是我试图转换的C代码:施工人员从C#到Objective-C的代码,c#,objective-c,constructor,C#,Objective C,Constructor,我们必须将C#代码转换为Objective-C代码,我很难弄清楚如何创建一个没有参数的构造函数,而另一个有两个参数。 这是我试图转换的C代码: namespace Account { class Program { public class Account { private double balance; private int accountNumber; public Account() {
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()
}
欢迎任何我能得到的帮助非常感谢,DannyObjective C与其他语言有点不同。与其他语言相比,它的语法更奇怪。虽然从我所看到的来看,你似乎还没有对objective-c了解多少。我建议你看看苹果的文档,或者买一本书来真正了解objective-c语法是如何工作的。学习语法应该不会花太长时间,但以下是您应该如何做到这一点
@interface NSObject : Account
{
@private
double balance;
int accountNumber;
}
-(void)account; //No Arguements
-(void)account:(int)accNum withBalance:(double)bal; //2 Arguements
@implementation Program
-(void)account
{
balance = 0;
accountNumber = 999;
}
-(void)account:(int)accNum withBalance:(double)bal
{
balance = bal;
accountNumber = accNum;
}
您只需提供两个初始值设定项,它们采用一般形式:
@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