Ví dụ, trong chương trình trước, bạn không cần phải tính toán các kết quả trung gian rem_4, rem_100, và rem_400-có thể thực hiện việc tính toán trực tiếp bên trong, nếu tuyên bố như sau Chú ý rằng bạn có thể gán giá trị ban đầu cho biến này giá trị bình thường, bạn gán cho charPtr | Categories 227 you could define your category s methods in a separate implementation section. In such a case the implementation section for these methods must also identify the category to which the methods with the interface section you do this by enclosing the category name inside parentheses after the class name like this implementation Fraction MathOps code for category methods @end In Program the interface and implementation sections for the new MathOps category are grouped together along with a test routine into a single file. Program MathOps Category and Test Program import interface Fraction MathOps - Fraction add Fraction f - Fraction mul Fraction f - Fraction sub Fraction f - Fraction div Fraction f @end implementation Fraction MathOps - Fraction add Fraction f To add two fractions a b c d a d b c b d Fraction result Fraction alloc init int resultNum resultDenom resultNum numerator denominator resultDenom denominator result setTo resultNum over resultDenom result reduce return result - Fraction sub Fraction f 228 Chapter 11 Categories and Protocols To sub two fractions a b - c d a d - b c b d Fraction result Fraction alloc init int resultNum resultDenom resultNum numerator - denominator resultDenom denominator result setTo resultNum over resultDenom result reduce return result - Fraction mul Fraction f Fraction result Fraction alloc init result setTo numerator over denominator result reduce return result - Fraction div Fraction f Fraction result Fraction alloc init result setTo numerator over denominator result reduce return result @end int main int argc char argv NSAutoreleasePool pool NSAutoreleasePool alloc init Fraction a Fraction alloc init Fraction b Fraction alloc init Fraction result a setTo 1 over 3 b setTo 2 over 5 a print NSLog @ b print NSLog @ ------- result a add b Categories 229 .