标签:
// 给Car类设计一个方法,用来和其它车比较车速,如果本车>其它车,返回1,如果本车<其它车,返回-1,相等返回0
// 设计另外一个方法,返回两个车速之差
#import <Foundation/Foundation.h>
@interface Car : NSObject
{
@public
float _speed;
}
- (int)compare:(Car *)newCar;
- (float)compare2:(float)speed;
@end
@implementation Car
- (int)compare:(Car *)newCar{
if(self->_speed > newCar->_speed){
return 1;
}else if(self->_speed < newCar->_speed){
return -1;
}else{
return 0;
}
}
- (float)compare2:(float)speed{
return self->_speed - speed;
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
Car *car1 = [Car new];
Car *car2 = [Car new];
car1->_speed = 120;
car2->_speed = 160;
int x = [car1 compare:car2];
NSLog(@"\n%d", x);
float y = [car1 compare2:160];
NSLog(@"\n%.3f", y);
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/lianfu/p/5149979.html