在我正在阅读的书中,他解释了如何在主文件中创建类别,但我试图弄清楚如何在单独的文件中创建这些类别..
我想为Fraction类创建一个名为“MathOperations”的类别,所以我打开了一个名为Fraction + MathOperations的新的.h和.m,因为我知道这是惯例。
这是Fraction.h
#import "Fraction.h"
@interface Fraction : NSObject
@property int numerator, denumerator;
-(void) print: (BOOL) test;
-(void) setTo: (int) n over: (int) d;
-(void) reduce;
-(double) convertToNum;
-(id) addFrac:(id)f;
-(void) justPrint;
-(id) initWith:(int)n over:(int)f;
+(Fraction *) allocF;
+(int) count;
+(int) gCounter;
@end
分数+ MathOperations.h
#import "Fraction.h"
@interface Fraction (MathOperations)
-(Fraction *) add: (Fraction *) f;
-(Fraction *) substract: (Fraction *) f;
-(Fraction *) multiply: (Fraction *) f;
-(Fraction *) divide: (Fraction *) f;
@end
分数+ MathOperations.m
#import "Fraction.h"
@implementation Fraction (MathOperations)
-(Fraction *)add:(Fraction *)f
{
extern int gCounter;
Fraction *result = [[Fraction alloc] init];
result.numerator = numerator * f.denumerator + denumerator * f.numerator;
result.denumerator = denumerator * f.denumerator;
[result reduce];
++gCounter;
return result;
}
-(Fraction *)substract:(Fraction *)f
{
Fraction *result = [[Fraction alloc] init];
result.numerator = numerator * f.denumerator - denumerator * f.numerator;
result.denumerator = denumerator * f.denumerator;
//[result reduce];
return result;
}
-(Fraction *)multiply:(Fraction *)f
{
Fraction *result = [[Fraction alloc] init];
result.numerator = numerator * f.numerator;
result.denumerator = denumerator * f.denumerator;
//[result reduce];
return result;
}
-(Fraction *)divide:(Fraction *)f
{
Fraction *result = [[Fraction alloc] init];
result.numerator = numerator * f.denumerator;
result.denumerator = denumerator * f.numerator;
//[result reduce];
return result;
}
@end
显然我正在宣布一些错误,因为我得到了一些错误,即变量未被声明..也就是说,在原始的Fraction类中声明新接口和实现更合理吗?
错误:
在Fraction类中我有一些名为“numerator”和“denumerator”的属性,它们似乎不会在meth实现文件中声明 TNX
答案 0 :(得分:3)
您应该为每个主要类的属性使用self.
前缀,例如self.numerator
。类别没有对属性的任何“内部”访问权限,它的行为与任何外部用户一样。
答案 1 :(得分:1)
我想简单地改变一下:
#import "Fraction.h"
到此:
#import "Fraction+MathOperations.h"
Fraction+MathOperations.m
中的可以解决问题。
答案 2 :(得分:1)
除了很少的错误,你的代码几乎是正确的。
在文件中找到更正为和替换
//
// Fraction+MathOperations.m
// prog3
//
// Created by niroohayon on 18/02/13.
// Copyright (c) 2013 niroohayon . All rights reserved.
//
#import "Fraction.h"
@implementation Fraction (MathOperations)
-(Fraction *)add:(Fraction *)f
{
//extern int gCounter;
Fraction *result = [[Fraction alloc] init];
result.numerator = self.numerator * f.denumerator + self.denumerator * f.numerator;
result.denumerator = self.denumerator * f.denumerator;
[result reduce];
// ++gCounter;
return result;
}
-(Fraction *)substract:(Fraction *)f
{
Fraction *result = [[Fraction alloc] init];
result.numerator = self.numerator * f.denumerator - self.denumerator * f.numerator;
result.denumerator = self.denumerator * f.denumerator;
//[result reduce];
return result;
}
-(Fraction *)multiply:(Fraction *)f
{
Fraction *result = [[Fraction alloc] init];
result.numerator = self.numerator * f.numerator;
result.denumerator = self.denumerator * f.denumerator;
//[result reduce];
return result;
}
-(Fraction *)divide:(Fraction *)f
{
Fraction *result = [[Fraction alloc] init];
result.numerator = self.numerator * f.denumerator;
result.denumerator = self.denumerator * f.numerator;
//[result reduce];
return result;
}
@end