我想在Class(Fraction *)返回一个变量Result

时间:2015-07-24 10:41:43

标签: objective-c

- (Fraction *)add:(Fraction *)addFra {

    Fraction *numerator1 = [[Fraction alloc]initWithFraction:[self initWithNumerator:self.num]];

    Fraction *numerator2 = [[Fraction alloc]initWithFraction:[self initWithNumerator:addFra.num]];

    Fraction *denominator1 = [[Fraction alloc]initWithFraction:[self initWithDenominator:self.den]];

    Fraction *denominator2 = [[Fraction alloc]initWithFraction:[self initWithDenominator:addFra.den]];

    Fraction *result = (numerator1 * denominator2) + (numerator2 * denominator1);

    return result;
}

1 个答案:

答案 0 :(得分:0)

Objective-C没有运算符重载,所以这些数学运算必须使用方法实现:

Fraction *result = [[numerator1 multiply:denominator2] add:[numerator2 multiply:denominator1]];

我还没有展示multiply:add:的实施情况,因为坦率地说,我无法理解如何正确地做到这一点。