[关闭]
@fiy-fish 2015-07-19T19:03:12.000000Z 字数 3412 阅读 1269

在此处输入标题

未分类


  1. // main.m
  2. // day09-03-动物园
  3. //
  4. // Created by Aaron on 15/7/13.
  5. // Copyright (c) 2015年 Aaron. All rights reserved.
  6. //
  7. #import <Foundation/Foundation.h>
  8. //练习: 有一个动物收容所,可以收留各种动物,动物收容所会按照动物的身高来排序
  9. //往收容所添加动物,排序,输出排序后的动物信息
  10. #import "AnimalZoon.h"
  11. #import "Dog.h"
  12. #import "Cat.h"
  13. #import "Car.h"
  14. int main(int argc, const char * argv[]) {
  15. @autoreleasepool {
  16. AnimalZoon *zoon = [[AnimalZoon alloc] init];
  17. NSArray *animalType = @[[Dog class],[Cat class],[Car class]];
  18. for(int i = 0; i < 10; i++)
  19. {
  20. NSInteger height = i+10;
  21. id <AnimalZoonHeight> animal = [[animalType[arc4random_uniform(3)] alloc] init];
  22. [animal setHeight:height];
  23. //[zoon addAniaml:animal];
  24. [zoon addSomeBody:animal];
  25. }
  26. [zoon sortByHeight];
  27. [zoon showInfo];
  28. }
  29. return 0;
  30. }

--

  1. #import <Foundation/Foundation.h>
  2. //#import "Animal.h"
  3. @protocol AnimalZoonHeight <NSObject>
  4. -(NSInteger)heightForMe;
  5. -(void)setHeight:(NSInteger)height;
  6. @end
  7. @interface AnimalZoon : NSObject
  8. -(void)addSomeBody:(id <AnimalZoonHeight>)obj;
  9. //-(void)addAniaml:(Animal *)animal;
  10. -(void)sortByHeight;
  11. -(void)showInfo;
  12. @end

  1. #import "AnimalZoon.h"
  2. #import "Animal.h"
  3. @interface AnimalZoon ()
  4. @property (nonatomic,retain) NSMutableArray *animalArray;
  5. @end
  6. @implementation AnimalZoon
  7. -(instancetype)init
  8. {
  9. if(self = [super init])
  10. {
  11. _animalArray = [NSMutableArray array];
  12. }
  13. return self;
  14. }
  15. -(void)addAniaml:(Animal *)animal
  16. {
  17. [_animalArray addObject:animal];
  18. }
  19. -(void)addSomeBody:(id <AnimalZoonHeight>)obj
  20. {
  21. //对象是否遵循了协议
  22. if([obj conformsToProtocol:@protocol(AnimalZoonHeight)])
  23. {
  24. [_animalArray addObject:obj];
  25. }
  26. }
  27. #if 0
  28. -(void)sortByHeight
  29. {
  30. for(int i = 0; i < _animalArray.count-1; i++)
  31. {
  32. for(int j = 0; j < _animalArray.count-1-i; j++)
  33. {
  34. if([(Animal *)_animalArray[j] heightForAnimal] < [(Animal *)_animalArray[j+1] heightForAnimal])
  35. {
  36. [_animalArray exchangeObjectAtIndex:j withObjectAtIndex:j+1];
  37. }
  38. }
  39. }
  40. }
  41. #endif
  42. -(void)sortByHeight
  43. {
  44. for(int i = 0; i < _animalArray.count-1; i++)
  45. {
  46. for(int j = 0; j < _animalArray.count-1-i; j++)
  47. {
  48. id <AnimalZoonHeight> obj1 = _animalArray[j];
  49. id <AnimalZoonHeight> obj2 = _animalArray[j+1];
  50. if([obj1 respondsToSelector:@selector(heightForMe)] && [obj2 respondsToSelector:@selector(heightForMe)])
  51. {
  52. if([obj1 heightForMe] < [obj2 heightForMe])
  53. {
  54. [_animalArray exchangeObjectAtIndex:j withObjectAtIndex:j+1];
  55. }
  56. }
  57. }
  58. }
  59. }
  60. -(void)showInfo
  61. {
  62. for(id obj in _animalArray)
  63. {
  64. NSLog(@"%@",obj);
  65. }
  66. }
  67. @end

  1. #import <Foundation/Foundation.h>
  2. #import "AnimalZoon.h"
  3. //父类遵循了协议,子类也遵循
  4. @interface Animal : NSObject <AnimalZoonHeight>
  5. {
  6. NSInteger _height;
  7. }
  8. @property (nonatomic,copy) NSString *name;
  9. -(void)setHeight:(NSInteger)height;
  10. -(NSInteger)heightForAnimal;
  11. @end

  1. #import "Animal.h"
  2. @implementation Animal
  3. -(void)setHeight:(NSInteger)height
  4. {
  5. if(_height != height)
  6. {
  7. _height = height;
  8. }
  9. }
  10. -(NSInteger)heightForAnimal
  11. {
  12. return _height;
  13. }
  14. -(NSInteger)heightForMe
  15. {
  16. return _height;
  17. }
  18. -(NSString *)description
  19. {
  20. return [NSString stringWithFormat:@"%@--height=%ld",_name,_height];
  21. }
  22. @end

  1. #import "Animal.h"
  2. @interface Dog : Animal
  3. @end

  1. #import "Dog.h"
  2. @implementation Dog
  3. -(instancetype)init
  4. {
  5. if(self = [super init])
  6. {
  7. self.name = @"狗子";
  8. }
  9. return self;
  10. }
  11. @end

  1. #import "Animal.h"
  2. @interface Cat : Animal
  3. @end

--

  1. #import "Cat.h"
  2. @implementation Cat
  3. -(instancetype)init
  4. {
  5. if(self = [super init])
  6. {
  7. self.name = @"猫子";
  8. }
  9. return self;
  10. }
  11. @end

  1. #import <Foundation/Foundation.h>
  2. #import "AnimalZoon.h"
  3. @interface Car : NSObject <AnimalZoonHeight>
  4. {
  5. NSInteger _height;
  6. }
  7. @property (nonatomic,copy) NSString *name;
  8. -(void)setHeight:(NSInteger)height;
  9. @end

  1. #import "Car.h"
  2. @implementation Car
  3. -(instancetype)init
  4. {
  5. if(self = [super init])
  6. {
  7. _name = @"小汽车";
  8. }
  9. return self;
  10. }
  11. -(void)setHeight:(NSInteger)height
  12. {
  13. if(_height != height)
  14. {
  15. _height = height;
  16. }
  17. }
  18. -(NSInteger)heightForMe
  19. {
  20. return _height;
  21. }
  22. -(NSString *)description
  23. {
  24. return [NSString stringWithFormat:@"%@--height=%ld",_name,_height];
  25. }
  26. @end
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注