全局变量与静态变量

作者:追风剑情 发布于:2019-2-25 10:36 分类:Objective-C

全局变量:A文件中定义的全局变量能被其他B、C、D、...等文件访问。
静态变量:A文件中定义的静态变量只能被A文件中的代码访问。

示例一:全局变量

Foo.h

  1. #import <Foundation/Foundation.h>
  2.  
  3. @interface Foo : NSObject
  4. -(void) setgGlobalVar: (int) val;
  5. @end


Foo.m

  1. #import "Foo.h"
  2.  
  3. // 如果此文件中有多处需要使用gGlobalVar,
  4. // 也可以在文件开始处声明,函数中直接使用即可。
  5. // extern int gGlobalVar;
  6.  
  7. @implementation Foo
  8.  
  9. -(void) setgGlobalVar: (int) val
  10. {
  11. // extern关键字告诉编译器这是全局变量
  12. extern int gGlobalVar;
  13. gGlobalVar = val;
  14. }
  15.  
  16. @end


main.m

  1. #import <Foundation/Foundation.h>
  2. #import "Foo.h"
  3.  
  4. // 全局变量
  5. int gGlobalVar = 5;
  6.  
  7. int main(int argc, const char * argv[]) {
  8. @autoreleasepool {
  9. Foo *myFoo = [[Foo alloc] init];
  10. NSLog(@"%i", gGlobalVar);
  11. [myFoo setgGlobalVar: 100];
  12. NSLog(@"%i", gGlobalVar);
  13. }
  14. return 0;
  15. }

运行测试
111.png

示例二:静态变量

Fraction.h

  1. #import <Foundation/Foundation.h>
  2.  
  3. @interface Fraction : NSObject
  4.  
  5. //@property指令让编译器自动为numerator、denominator生成getter、setter方法
  6. @property int numerator, denominator;
  7.  
  8. -(void) print;
  9. -(double) convertToNum;
  10. -(void) setTo: (int) n over: (int) d;
  11. -(void) set: (int) n : (int) d;
  12. -(Fraction *) add: (Fraction *) f;
  13. -(void) reduce;
  14. +(Fraction *) allocF;
  15. +(int) count;
  16.  
  17. @end


Fraction.m

  1. #import "Fraction.h"
  2.  
  3. @implementation Fraction
  4.  
  5. @synthesize numerator, denominator;
  6.  
  7. // 用静态变量记录创建了多少个Fraction对象
  8. // 静态变量不能被文件以外的代码访问
  9. static int gCounter;
  10.  
  11. -(void) print
  12. {
  13. NSLog(@"%i/%i", numerator, denominator);
  14. }
  15.  
  16. // 多个参数的方法
  17. -(void) setTo: (int) n over: (int) d
  18. {
  19. numerator = n;
  20. denominator = d;
  21. }
  22.  
  23. -(double) convertToNum
  24. {
  25. if (denominator != 0)
  26. return (double) numerator / denominator;
  27. else
  28. return NAN;
  29. }
  30.  
  31. // 省略参数名的多个参数方法
  32. // 注意,第一个参数名不能省
  33. // 省略参数名不是一种好的编程风格,因为它使程序很难读懂并且很不直观,特别是参数很重要时。
  34. -(void) set: (int) n : (int) d
  35. {
  36. numerator = n;
  37. denominator = d;
  38. }
  39.  
  40. // 分数相加
  41. -(Fraction *) add: (Fraction *) f
  42. {
  43. // 添加两个分数
  44. // a/b+c/d=((a*d)+(b*c))/(b*d)
  45. // 创建一个新对象来存储结果
  46. Fraction *result = [[Fraction alloc] init];
  47. result.numerator = numerator * f.denominator + denominator * f.numerator;
  48. result.denominator = denominator * f.denominator;
  49. [result reduce];
  50. return result;
  51. }
  52.  
  53. // 约分
  54. -(void) reduce
  55. {
  56. int u = numerator;
  57. int v = denominator;
  58. int temp;
  59. while (v != 0) {
  60. temp = u % v;
  61. u = v;
  62. v = temp;
  63. }
  64. numerator /= u;
  65. denominator /= u;
  66. }
  67.  
  68. +(Fraction *) allocF;
  69. {
  70. // 可以省略extern声明,写上也不会错
  71. //extern int gCounter;
  72. ++gCounter;
  73. return [Fraction alloc];
  74. }
  75.  
  76. +(int) count
  77. {
  78. //extern int gCounter;
  79. return gCounter;
  80. }
  81.  
  82. @end


main.m

  1. #import <Foundation/Foundation.h>
  2. #import "Fraction.h"
  3.  
  4. int main(int argc, const char * argv[]) {
  5. @autoreleasepool {
  6. Fraction *a, *b, *c;
  7. NSLog(@"Fractions allocated: %i", [Fraction count]);
  8. a = [[Fraction allocF] init];
  9. b = [[Fraction allocF] init];
  10. c = [[Fraction allocF] init];
  11. NSLog(@"Fractions allocated: %i", [Fraction count]);
  12. }
  13. return 0;
  14. }

运行测试
222.png

标签: Objective-C

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号