2011-10-08 2 views
3

우리는 C# 코드를 Objective-C 코드로 변환해야하고, 하나의 생성자를 인자없이 생성하는 방법과 2 개의 인자를 생성하는 방법을 찾는 데 어려움을 겪고있었습니다. 이것은 내가 변환하려고 C# 코드입니다 :생성자를위한 C#에서 Objective-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; 
     } 
     } 
} 

}

을 그리고 그것은

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

@public Account() 
    } 
도 정확한지 여부를 경우이 내가 목표 C 확실하지 위해 지금까지 무엇을 가지고

대니에게 감사의 말을 전합니다.

답변

0

목표 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; 

} 
 
당신은 단순히 일반적인 형식 소요이 초기화, 제공
3

:

@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 
관련 문제