车和汽车
1.设计2个类,类之间的关系自拟(比如继承、组合)
1> 车
(1)属性
轮子数
速度
(2)方法
属性相应的set和get方法
2> 客车
(1)属性
轮子数
速度
座位数
(2)方法
属性相应的set和get方法
/—————————————-/
car.h
#ifndef car_h
#define car_h
#import <Foundation/Foundation.h>
@interface Car: NSObject
{
@private
int _wheels;
int _speed;
}
- (void)setWheels:(int) wheels;
- (int)getWheels;
- (void)setSpeed:(int) speed;
- (int)getSpeed;
@end
#endif /* car_h */
car.m
#import <Foundation/Foundation.h>
#import "car.h"
@implementation Car
- (void)setWheels:(int) wheels
{
_wheels = wheels;
}
-(int)getWheels
{
return _wheels;
}
- (void)setSpeed:(int) speed
{
_speed = speed;
}
- (int)getSpeed
{
return _speed;
}
@end
bus.h
#ifndef bus_h
#define bus_h
#import "car.h"
#import <Foundation/Foundation.h>
@interface Bus:Car
{
@private
int _seats;
}
- (void)setSeats:(int) seats;
- (int)getSeats;
@end
#endif
/* bus_h */
bus.m
#import "bus.h"
#import "car.h"
@implementation Bus
- (void) setSeats:(int) seats
{
_seats = seats;
}
- (int)getSeats
{
return _seats;
}
@end
main.m
#import <Foundation/Foundation.h>
#import "car.h"
#import "bus.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
Car *bus = [Bus new];
[bus setWheels:4];
[bus setSpeed:10];
NSLog(@"bus 轮子数:%d,速度:%d",[bus getWheels],[bus getSpeed]);
}
return 0;
}