什么是组织以下程序的理想方式:
#import <Foundation/Foundation.h>
// ---- @interface section ----
@interface Fraction: NSObject
-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
@end
// --- @implementation section ---
@implementation Fraction
{
int numerator;
int denominator;
}
-(void) print
{
NSLog(@"%i/%i", numerator,denominator);
}
-(void) setNumerator:(int)n
{
numerator = n;
}
-(void) setDenominator:(int)d
{
denominator = d;
}
@end
// -- Program section --
int main(int argc, const char * argv[])
{
@autoreleasepool {
Fraction *frac1 = [[Fraction alloc] init];
Fraction *frac2 = [[Fraction alloc] init];
[frac1 setNumerator: 2];
[frac1 setDenominator: 3];
[frac1 print];
}
return 0;
}
我在问这个菜鸟问题,因为在 X-Code 中,有一个规定可以让你的课程分开,而我正在学习的书(使用 Objective-C 编程)没有指定将哪个代码块放在哪里。