首页 > 学院 > 开发设计 > 正文

从零开始学习Object-C---第四天(3)

2019-11-14 19:53:50
字体:
来源:转载
供稿:网友

今天首先是总结之前的数据类型:

注:id数据类型是OC的一种特殊的数据类型,可用来存储任何数据类型,是多态和动态绑定的基础。

开始新的讲解--算术表达式

一段代码开始今天的学习:

 1 // 2 //  main.m 3 //  Demo5 4 // 5 //  Created by lee on 14/11/6. 6 //  Copyright (c) 2014年 lee. All rights reserved. 7 // 8  9 #import <Foundation/Foundation.h>10 11 int main(int argc, const char * argv[]) {12     @autoreleasepool {13         int a = 100;14         int b = 2;15         int c = 25;16         int d = 4;17         int result;18         result = a-b;19         NSLog(@"a - b = %i", result);20         21         result = b * c;22         NSLog(@"b * c = %i", result);23         24         result = a / c;25         NSLog(@"a / c = %i", result);26         27         result = a + b * c;28         NSLog(@"a + b * c = %i", result);29         30         result = a * b + c * d;31         NSLog(@"a * b + c * d = %i", result);32     }33     return 0;34 }35 输出结果:36 2014-11-06 01:16:40.458 Demo5[651:34029] a - b = 9837 2014-11-06 01:16:40.458 Demo5[651:34029] b * c = 5038 2014-11-06 01:16:40.458 Demo5[651:34029] a / c = 439 2014-11-06 01:16:40.459 Demo5[651:34029] a + b * c = 15040 2014-11-06 01:16:40.459 Demo5[651:34029] a * b + c * d = 30041 PRogram ended with exit code: 0
View Code

注:OC中算法优先级和其他语言的优先级相同,先乘除后加减,括号能改变计算顺序

运用算数表达式的规则写出计算器类,来实现基本的算术:

首先定义接口:包含 初始化(clear),get set方法,加(add)减(subtract)乘(mutiply)除(devide)方法定义

 1 // 2 //  NSObject+Calculator.h 3 //  Demo5 4 // 5 //  Created by lee on 14/11/6. 6 //  Copyright (c) 2014年 lee. All rights reserved. 7 // 8  9 #import <Foundation/Foundation.h>10 11 @interface Calculator : NSObject12 {13     double accumulator;14 }15 //accmulator16 -(void) setAccumulator: (double) value;17 -(void) clear;18 -(double) getAccumulator;19 20 //arithetic methods21 -(void) add: (double) value;22 -(void) subtract: (double) value;23 -(void) multiply: (double) value;24 -(void) devide: (double) value;25 @end
View Code

然后定义实现类,实现接口中定义的方法

 1 // 2 //  NSObject+Calculator.m 3 //  Demo5 4 // 5 //  Created by lee on 14/11/6. 6 //  Copyright (c) 2014年 lee. All rights reserved. 7 // 8  9 #import "NSObject+Calculator.h"10 11 @implementation Calculator12 -(void) setAccumulator:(double)value13 {14     accumulator = value;15 }16 17 -(void) clear18 {19     accumulator = 0;20 }21 22  -(double) getAccumulator23 {24     return accumulator;25 }26 27 -(void) add:(double)value28 {29     accumulator += value;30 }31 32 -(void) subtract:(double)value33 {34     accumulator -= value;35 }36 37 -(void) multiply:(double)value38 {39     accumulator *= value;40 }41 42 -(void) devide:(double)value43 {44     accumulator /= value;45 }46 47 @end
View Code

最后方法调用以及输出结果

 1 // 2 //  main.m 3 //  Demo5 4 // 5 //  Created by lee on 14/11/6. 6 //  Copyright (c) 2014年 lee. All rights reserved. 7 // 8  9 #import <Foundation/Foundation.h>10 #import "NSObject+Calculator.h"11 12 int main(int argc, const char * argv[]) {13     @autoreleasepool {14         Calculator *deskCalc;15         deskCalc = [[Calculator alloc] init];16         17         [deskCalc clear];18         [deskCalc setAccumulator: 10];19         NSLog(@"The result id %g", [deskCalc getAccumulator]);20         [deskCalc add: 200];21         NSLog(@"The result id %g", [deskCalc getAccumulator]);22         [deskCalc devide: 15.0];23         NSLog(@"The result id %g", [deskCalc getAccumulator]);24         [deskCalc subtract: 10.0];25         NSLog(@"The result id %g", [deskCalc getAccumulator]);26         [deskCalc multiply: 5];27         NSLog(@"The result is %g", [deskCalc getAccumulator]);28     }29     return 0;30 }31 输出结果:32 2014-11-06 00:50:19.874 Demo5[601:25952] The result id 1033 2014-11-06 00:50:19.875 Demo5[601:25952] The result id 21034 2014-11-06 00:50:19.875 Demo5[601:25952] The result id 1435 2014-11-06 00:50:19.875 Demo5[601:25952] The result id 436 2014-11-06 00:50:19.875 Demo5[601:25952] The result is 2037 Program ended with exit code: 0
View Code

类和方法定义和使用参考:http://www.VEVb.com/likun-java/p/4058448.html


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表