@fiy-fish
2015-07-14T19:43:44.000000Z
字数 859
阅读 1223
Objective-c
// main.m
// 狗比较速度
//
// Created by Aaron on 15/7/2.
// Copyright (c) 2015年 Aaron. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Dog.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Dog *d1 = [[Dog alloc] initWithSpeed:5];
Dog *d2 = [[Dog alloc] initWithSpeed:10];
NSLog(@"%ld",[d2 compareWithOtherDog:d1]);
}
return 0;
}
import <Foundation/Foundation.h>
/*
设计一个狗类Dog,给类添加一个方法,该方法用来比较狗A与另外一只狗B的速度,如果A的速度快,返回1,如果B的速度快,返回-1,相同则返回0.
*/
@interface Dog : NSObject
{
NSInteger _speed;
}
-(instancetype)initWithSpeed:(NSInteger)speed;
-(NSInteger)compareWithOtherDog:(Dog *)dog;
@end
#import "Dog.h"
@implementation Dog
-(instancetype)initWithSpeed:(NSInteger)speed
{
if(self = [super init])
{
_speed = speed;
}
return self;
}
-(NSInteger)compareWithOtherDog:(Dog *)dog
{
if(_speed > [dog speed])
{
return 1;
}
else if(_speed == [dog speed])
{
return 0;
}
else
{
return -1;
}
}
-(NSInteger)speed
{
return _speed;
}
@end