[关闭]
@fiy-fish 2015-05-19T09:36:38.000000Z 字数 2965 阅读 1278

构造方法重载

Objective-c


  1. //
  2. // main.m
  3. // 构造方法重载
  4. //
  5. // Created by Aaron on 15/5/19.
  6. // Copyright (c) 2015年 Aaron. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. /*
  10. 类是用来描述对象的
  11. 对象是由类产生的一个实体
  12. 封装
  13. 1.找对象
  14. 2.分析属性
  15. 3.分析行为
  16. 4.创建类-----接口部分,实现部分
  17. 接口部分:定义属性 声明方法
  18. 实现部分:实现方法
  19. 5.通过类创建对象
  20. 6.利用对象完成功能
  21. @interface 类名:NSObject
  22. {
  23. 默认保护类型:只能在当前类或者其子类直接访问
  24. 私有:只能在当前类里面直接访问
  25. 公有:任何地方都可以直接访问(最不安全,违背了封装的思想)
  26. 定义属性(实例变量)
  27. }
  28. 定义方法
  29. -表示实例方法
  30. -(void)test;
  31. -(void)test:(int)num;
  32. OC没有私有类型的方法
  33. 但是可以通过不提供接口的方式来实现方法的私有化
  34. 提供setter getter接口来实现对属性的访问
  35. -(void)set实例变量名:(类型)参数;
  36. -(类型)实例变量名;
  37. @end
  38. @implementation 类名
  39. 实现方法
  40. -(int)test
  41. {
  42. static int i = 100;
  43. return i;
  44. }
  45. @end
  46. */
  47. #import "Shoose.h"
  48. #import "Dog.h"
  49. int main(int argc, const char * argv[])
  50. {
  51. @autoreleasepool {
  52. Shoose *s = [[Shoose alloc] init];
  53. [s showMe];
  54. Shoose *s1 = [[Shoose alloc] initWithColor:@"黑色"];
  55. [s1 showMe];
  56. Dog *dog1 = [[Dog alloc] initWithSpeed:99];
  57. Dog *dog2 = [[Dog alloc] initWithSpeed:200];
  58. NSLog(@"%d",[dog1 compareWithOtherDog:dog2]);
  59. }
  60. return 0;
  61. }
  1. // Shoose.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 Shoose : NSObject
  9. {
  10. NSString *_color;
  11. }
  12. -(void)showMe;
  13. //-(id)init; 如果是系统的init方法,可以不用声明
  14. //如果是自己重载的构造方法,需要声明
  15. -(id)initWithColor:(NSString *)color;
  16. @end
  1. // Shoose.m
  2. // 构造方法重载
  3. //
  4. // Created by Aaron on 15/5/19.
  5. // Copyright (c) 2015年 Aaron. All rights reserved.
  6. //
  7. #import "Shoose.h"
  8. @implementation Shoose
  9. //id泛型的对象指针
  10. -(id)init
  11. {
  12. if(self = [super init])
  13. {
  14. _color = @"白色";
  15. }
  16. return self;
  17. }
  18. -(id)initWithColor:(NSString *)color
  19. {
  20. if(self = [super init])
  21. {
  22. _color = color;
  23. }
  24. return self;
  25. }
  26. -(void)showMe
  27. {
  28. //%@输出OC的字符串
  29. NSLog(@"这是一个%@的鞋子",
  30. _color);
  31. }
  32. @end
  1. // Dog.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. /*
  9. 设计一个狗类Dog,给类添加一个方法,该方法用来比较狗A与另外一只狗B的速度,如果A的速度快,返回1,如果B的速度快,返回-1,相同则返回0.
  10. */
  11. @interface Dog : NSObject
  12. {
  13. int _speed;
  14. int _weight;
  15. }
  16. //-(void)setSpeed:(int)speed;
  17. //-(void)setWeight:(int)weight;
  18. -(void)setSpeed:(int)speed andWeight:(int)weight;
  19. -(id)initWithSpeed:(int)speed;
  20. //带多个参数的方法
  21. //:表示一个参数 有多个参数就要带多个:
  22. //withWeight 叫做标签
  23. //initWithSpeed:withWeight:
  24. -(id)initWithSpeed:(int)speed withWeight:(int)weight;
  25. -(int)compareWithOtherDog:(Dog *)otherDog;
  26. @end
  1. // Dog.m
  2. // 构造方法重载
  3. //
  4. // Created by Aaron on 15/5/19.
  5. // Copyright (c) 2015年 Aaron. All rights reserved.
  6. //
  7. #import "Dog.h"
  8. @implementation Dog
  9. -(id)init
  10. {
  11. // if(self = [super init])
  12. // {
  13. // _speed = 10000;
  14. // }
  15. self = [self initWithSpeed:10000 withWeight:0];
  16. return self;
  17. }
  18. -(id)initWithSpeed:(int)speed
  19. {
  20. // if(self = [super init])
  21. // {
  22. // _speed = speed;
  23. // }
  24. self = [self initWithSpeed:speed withWeight:0];
  25. return self;
  26. }
  27. -(id)initWithSpeed:(int)speed withWeight:(int)weight
  28. {
  29. if(self = [super init])
  30. {
  31. _speed = speed;
  32. _weight = weight;
  33. }
  34. return self;
  35. }
  36. -(int)compareWithOtherDog:(Dog *)otherDog
  37. {
  38. //在类的内部,用对象调用实例方法
  39. [otherDog test1];
  40. //也可以通过self指针来调用
  41. //要看取到谁的实例变量,看谁是消息的接收者
  42. [self test1];
  43. if(_speed > otherDog->_speed)
  44. {
  45. return 1;
  46. }
  47. else if(_speed == otherDog->_speed)
  48. {
  49. return 0;
  50. }
  51. else
  52. {
  53. return -1;
  54. }
  55. }
  56. -(void)test1
  57. {
  58. NSLog(@"test1 speed = %d",_speed);
  59. }
  60. -(void)setSpeed:(int)speed
  61. {
  62. if(_speed != speed)
  63. {
  64. _speed = speed;
  65. }
  66. }
  67. -(void)setWeight:(int)weight
  68. {
  69. if(_weight != weight)
  70. {
  71. _weight = weight;
  72. }
  73. }
  74. -(void)setSpeed:(int)speed andWeight:(int)weight
  75. {
  76. [self setSpeed:speed];
  77. [self setWeight:weight];
  78. }
  79. @end
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注