【练习】车速

main.h

#import

#import "Car.h"

int main(int argc, const char * argv[])

{

Car *c1 = [Car new];

c1->speed = 40;

Car *c2 = [Car new];

c2->speed = 50;

int d = [c1 comparSpeedWithOther:c2];

NSLog(@"车速差是%d",d);

return 0;

}



//  一个Car类,设计一个方法用来比较两个车的速度,返回车速的差

Car.h

#import

@interface Car : NSObject

{

@public

int speed;

}


- (int)comparSpeedWithOther:(Car *)otherCar;

//  Car *星号必不可少

{

return speed - otherCar->speed;

}

@end


Car.m

#import

@implementation Car

- (int)comparSpeedWithOther:(Car *)otherCar

{

//  otherCar->speed 获取对象的属性

return speed - otherCar->speed;

}

@end

你可能感兴趣的:(【练习】车速)