,如果我有一类名为将方法设置为对象或甚至将方法参数设置为对象意味着什么?
@interface myClass : NSObject {
int firstnum;
int secondnum;
}
//an a method that looks like this
-(myClass *) myMethod (myClass *) f;
@end
那有什么方法回报?即时通讯过去看到像(int)myMethod知道它返回一个整数。但是当它在这里返回一个对象,比如类名时,它可能会返回什么呢?如果你想写出正在工作/学习的整个项目,就不好写了。 (有些方法已被截断,以保持问题简单,但如果你想生病后它让我知道,日Thnx
#import <Foundation/Foundation.h>
@interface Fraction : NSObject {
int numerator;
int denominator;
}
@property int numerator, denominator;
-(Fraction *) add: (Fraction *) f;
@end
#import "Fraction.h"
@implementation Fraction
@synthesize numerator, denominator;
-(Fraction *) add: (Fraction *) f
{
Fraction *result = [[Fraction alloc] init];
// To add two fractions:
// a/b + c/d = ((a*d) + (b*c))/(b * d)
result.numerator = numerator * f.denominator + denominator * f.numerator;
result.denominator = denominator * f.denominator;
[result reduce];
return result;
}
@end
哦,也请用大写字母! :)在你的问题,那是;您的代码大部分遵循Obj-C约定。 (虽然'firstnum'和'secondnum'通常会驼峰likeThis) – andyvn22
你需要一个冒号( ':')myMethod'后' – Felixyz