[关闭]
@fiy-fish 2015-05-19T09:44:13.000000Z 字数 2032 阅读 1116

类方法

未分类


main.m

  1. // main.m
  2. // 类方法
  3. //
  4. // Created by Aaron on 15/5/19.
  5. // Copyright (c) 2015年 Aaron. All rights reserved.
  6. //
  7. #import <Foundation/Foundation.h>
  8. #import "Person.h"
  9. #import "Tool.h"
  10. int main(int argc, const char * argv[])
  11. {
  12. @autoreleasepool {
  13. //由类来调用的方法 类方法 +
  14. Person *p = [[Person alloc] init];
  15. //类是一个类类型的对象
  16. //Class
  17. Person *p1 = [[Person alloc] init];
  18. Person *p2 = [[Person alloc] init];
  19. //1.创建对象
  20. //2.作为工具
  21. [Person test1];
  22. Tool *tool = [[Tool alloc] init];
  23. [tool setNum1:9 andNum2:10 andC:'/'];
  24. NSLog(@"%d",[tool getResult]);
  25. NSLog(@"++++: %d",[Tool resultForNum1:9 andNum2:10 andC:'+']);
  26. }
  27. return 0;
  28. }

Person.h

  1. // Person.h
  2. // 类方法
  3. //
  4. // Created by Aaron on 15/5/19.
  5. // Copyright (c) 2015年 Aaron. All rights reserved.
  6. //
  7. #import <Foundation/Foundation.h>
  8. @interface Person : NSObject
  9. {
  10. int _age;
  11. }
  12. //+表示类方法
  13. //由类来调用的
  14. +(void)test1;
  15. @end

Tool.h

  1. // Tool.h
  2. // 类方法
  3. //
  4. // Created by Aaron on 15/5/19.
  5. // Copyright (c) 2015年 Aaron. All rights reserved.
  6. //
  7. #import <Foundation/Foundation.h>
  8. @interface Tool : NSObject
  9. {
  10. int _num1;
  11. int _num2;
  12. char _c;
  13. }
  14. -(void)setNum1:(int)num1 andNum2:(int)num2 andC:(char)c;
  15. -(int)getResult;
  16. +(int)resultForNum1:(int)num1 andNum2:(int)num2 andC:(char)c;
  17. @end

Tool.m

  1. // Tool.m
  2. // 类方法
  3. //
  4. // Created by Aaron on 15/5/19.
  5. // Copyright (c) 2015年 Aaron. All rights reserved.
  6. //
  7. #import "Tool.h"
  8. @implementation Tool
  9. -(void)setNum1:(int)num1 andNum2:(int)num2 andC:(char)c
  10. {
  11. _num1 = num1;
  12. _num2 = num2;
  13. _c = c;
  14. }
  15. -(int)getResult
  16. {
  17. int temp = 0;
  18. switch(_c)
  19. {
  20. case '+':temp = _num1+_num2;
  21. break;
  22. case '-':temp = _num1-_num2;
  23. break;
  24. case '*':temp = _num1*_num2;
  25. break;
  26. case '/':temp = _num1/_num2;
  27. break;
  28. }
  29. //在实例方法里面:
  30. //可以直接使用类名来访问类方法
  31. return temp;
  32. }
  33. +(int)resultForNum1:(int)num1 andNum2:(int)num2 andC:(char)c
  34. {
  35. int temp = 0;
  36. switch(c)
  37. {
  38. case '+':temp = num1+num2;
  39. break;
  40. case '-':temp = num1-num2;
  41. break;
  42. case '*':temp = num1*num2;
  43. break;
  44. case '/':temp = num1/num2;
  45. break;
  46. }
  47. //在类方法里面:
  48. //不能直接使用实例变量
  49. //也不能使用self访问实例变量,因为self现在是类对象,不是实例
  50. //也不能直接用self调用实例方法
  51. //如果想要在类方法里面访问实例变量
  52. //需要提供实例对象
  53. Tool *tool = [[Tool alloc] init];
  54. NSLog(@"%d",tool->_num1);
  55. return temp;
  56. }
  57. @end

Person.m

  1. // Person.m
  2. // 类方法
  3. //
  4. // Created by Aaron on 15/5/19.
  5. // Copyright (c) 2015年 Aaron. All rights reserved.
  6. //
  7. #import "Person.h"
  8. @implementation Person
  9. +(void)load
  10. {
  11. NSLog(@"加载类");
  12. }
  13. +(void)initialize
  14. {
  15. NSLog(@"创建类对象");
  16. }
  17. +(void)test1
  18. {
  19. NSLog(@".....");
  20. }
  21. @end
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注