@fiy-fish
2015-07-19T19:03:12.000000Z
字数 3412
阅读 1287
未分类
// main.m
// day09-03-动物园
//
// Created by Aaron on 15/7/13.
// Copyright (c) 2015年 Aaron. All rights reserved.
//
#import <Foundation/Foundation.h>
//练习: 有一个动物收容所,可以收留各种动物,动物收容所会按照动物的身高来排序
//往收容所添加动物,排序,输出排序后的动物信息
#import "AnimalZoon.h"
#import "Dog.h"
#import "Cat.h"
#import "Car.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
AnimalZoon *zoon = [[AnimalZoon alloc] init];
NSArray *animalType = @[[Dog class],[Cat class],[Car class]];
for(int i = 0; i < 10; i++)
{
NSInteger height = i+10;
id <AnimalZoonHeight> animal = [[animalType[arc4random_uniform(3)] alloc] init];
[animal setHeight:height];
//[zoon addAniaml:animal];
[zoon addSomeBody:animal];
}
[zoon sortByHeight];
[zoon showInfo];
}
return 0;
}
--
#import <Foundation/Foundation.h>
//#import "Animal.h"
@protocol AnimalZoonHeight <NSObject>
-(NSInteger)heightForMe;
-(void)setHeight:(NSInteger)height;
@end
@interface AnimalZoon : NSObject
-(void)addSomeBody:(id <AnimalZoonHeight>)obj;
//-(void)addAniaml:(Animal *)animal;
-(void)sortByHeight;
-(void)showInfo;
@end
#import "AnimalZoon.h"
#import "Animal.h"
@interface AnimalZoon ()
@property (nonatomic,retain) NSMutableArray *animalArray;
@end
@implementation AnimalZoon
-(instancetype)init
{
if(self = [super init])
{
_animalArray = [NSMutableArray array];
}
return self;
}
-(void)addAniaml:(Animal *)animal
{
[_animalArray addObject:animal];
}
-(void)addSomeBody:(id <AnimalZoonHeight>)obj
{
//对象是否遵循了协议
if([obj conformsToProtocol:@protocol(AnimalZoonHeight)])
{
[_animalArray addObject:obj];
}
}
#if 0
-(void)sortByHeight
{
for(int i = 0; i < _animalArray.count-1; i++)
{
for(int j = 0; j < _animalArray.count-1-i; j++)
{
if([(Animal *)_animalArray[j] heightForAnimal] < [(Animal *)_animalArray[j+1] heightForAnimal])
{
[_animalArray exchangeObjectAtIndex:j withObjectAtIndex:j+1];
}
}
}
}
#endif
-(void)sortByHeight
{
for(int i = 0; i < _animalArray.count-1; i++)
{
for(int j = 0; j < _animalArray.count-1-i; j++)
{
id <AnimalZoonHeight> obj1 = _animalArray[j];
id <AnimalZoonHeight> obj2 = _animalArray[j+1];
if([obj1 respondsToSelector:@selector(heightForMe)] && [obj2 respondsToSelector:@selector(heightForMe)])
{
if([obj1 heightForMe] < [obj2 heightForMe])
{
[_animalArray exchangeObjectAtIndex:j withObjectAtIndex:j+1];
}
}
}
}
}
-(void)showInfo
{
for(id obj in _animalArray)
{
NSLog(@"%@",obj);
}
}
@end
#import <Foundation/Foundation.h>
#import "AnimalZoon.h"
//父类遵循了协议,子类也遵循
@interface Animal : NSObject <AnimalZoonHeight>
{
NSInteger _height;
}
@property (nonatomic,copy) NSString *name;
-(void)setHeight:(NSInteger)height;
-(NSInteger)heightForAnimal;
@end
#import "Animal.h"
@implementation Animal
-(void)setHeight:(NSInteger)height
{
if(_height != height)
{
_height = height;
}
}
-(NSInteger)heightForAnimal
{
return _height;
}
-(NSInteger)heightForMe
{
return _height;
}
-(NSString *)description
{
return [NSString stringWithFormat:@"%@--height=%ld",_name,_height];
}
@end
#import "Animal.h"
@interface Dog : Animal
@end
#import "Dog.h"
@implementation Dog
-(instancetype)init
{
if(self = [super init])
{
self.name = @"狗子";
}
return self;
}
@end
#import "Animal.h"
@interface Cat : Animal
@end
--
#import "Cat.h"
@implementation Cat
-(instancetype)init
{
if(self = [super init])
{
self.name = @"猫子";
}
return self;
}
@end
#import <Foundation/Foundation.h>
#import "AnimalZoon.h"
@interface Car : NSObject <AnimalZoonHeight>
{
NSInteger _height;
}
@property (nonatomic,copy) NSString *name;
-(void)setHeight:(NSInteger)height;
@end
#import "Car.h"
@implementation Car
-(instancetype)init
{
if(self = [super init])
{
_name = @"小汽车";
}
return self;
}
-(void)setHeight:(NSInteger)height
{
if(_height != height)
{
_height = height;
}
}
-(NSInteger)heightForMe
{
return _height;
}
-(NSString *)description
{
return [NSString stringWithFormat:@"%@--height=%ld",_name,_height];
}
@end