在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
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 }
|
请发表评论