1 /* 2 计算器类 3 1>返回π 4 2>计算两个整数的平方 5 3>计算两个整数的和 6 */ 7 8 9 #import <Foundation/Foundation.h> 10 11 @interface Calculator: NSObject 12 //方法名:pi 13 - (double) pi; 14 15 //OC方法中,一个参数对应一个冒号: 16 //方法名:squr: 17 - (int) squr:(int) num; 18 19 //方法名: sumWithNum1:andNum2: 20 - (int) sumWithNum1:(int)num1 andNum2:(int)num2; 21 22 @end 23 24 25 @implementation Calculator 26 - (double) pi 27 { 28 return 3.14; 29 } 30 31 - (int) squr: (int) num 32 { 33 return num * num; 34 } 35 36 - (int) sumWithNum1:(int)num1 andNum2:(int)num2; 37 { 38 return num1 + num2; 39 } 40 @end 41 42 43 int main() 44 { 45 Calculator *c = [Calculator new]; 46 int d1 = 4; 47 int d2 = 5; 48 int d3 = 9; 49 NSLog(@"PI = %f", [c pi]); 50 NSLog(@"%d 的平方是 %d",d1, [c squr: d1]); 51 NSLog(@"%d + %d = %d", d2, d3, [c sumWithNum1:d1 andNum2:d3]); 52 return 0; 53 }
时间: 2024-08-24 02:33:13