//设计一个计算器对象实现求两数的和,求两数的商 #include<Foundation/Foundation.h> @interface JiSuanQi : NSObject
+ (int)sumOfNum1 : (int)num1 andNum2 : (int)num2; + (int)averageOfNum1 :(int)num1 andNum2 : (int)num2; @end // JiSuanQi类 @implementation JiSuanQi + (int)sumOfNum1 : (int)num1 andNum2 : (int)num2 // 类方法实现两个数字相加 { return num1+num2; } + (int)averageOfNum1 :(int)num1 andNum2 : (int)num2 // 类方法实现两数的商 { int sum=[JiSuanQi sumOfNum1:num1 andNum2:num2]; return sum/2; } @end int main() {
int a=[JiSuanQi sumOfNum1:5 andNum2:3]; int b=[JiSuanQi averageOfNum1:5 andNum2:3]; NSLog(@"sum of num1 and num2 is %d",a); NSLog(@"average of num1 and num2 is %d",b); return 0;
}
|