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

发布于 2024-12-09 07:17:04 字数 736 浏览 0 评论 0原文

我们必须将 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()
   }

接受任何我能得到的帮助,非常感谢,Danny

We have to convert our C# code to Objective-C code and i was having a hard time figuring out how to create one constructor with no arguments while the other with 2 arguments.
This is the C# code that I am trying to convert:

     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;
        }
        }
}

}

And this is what I have so far for the Objective C not sure if it is even correct or not

     @interface classname : Account 
   {
@private double balance;
@private int accountNumber;

@public Account()
   }

Open to any help i can get thank you very much, Danny

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

够钟 2024-12-16 07:17:04

您只需提供两个初始值设定项,其采用一般形式:

@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

you simply provide two initializers, which takes the general form:

@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
葬花如无物 2024-12-16 07:17:04

Objective 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;

}

Objective C is a bit different from other languages. It has a weirder syntax compared to other languages. Though from what I've seen, it appears that you haven't learnt much about objective-c yet. I would recommend you look at apple's documentation or get a book to actually learn about how the objective-c syntax works. It shouldn't take too long to learn the syntax, but here's how you should do it.


@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;

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文