main.h
#import <Foundation/Foundation.h>
#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 <Foundation/Foundation.h>
@interface Car : NSObject
{
@public
int speed;
}
- (int)comparSpeedWithOther:(Car *)otherCar;
// Car *星号必不可少
{
return speed - otherCar->speed;
}
@end
Car.m
#import <Car.h>
@implementation Car
- (int)comparSpeedWithOther:(Car *)otherCar
{
// otherCar->speed 获取对象的属性
return speed - otherCar->speed;
}
@end