定义了一个Vehical类和一个Bus类,Bus类中包含一个Vehical的对象_vehical。 为什么不能在Bus的SetVehicalWithWheels andSpeed方法中设置_vehical的wheels和speed?代码如下: #import <Foundation/Foundation.h>
@interface Vehical : NSObject { int _wheels; double _speed; } - (void)SetWheels:(int)a; - (void)SetSpeed:(double)b; - (int)Wheels; - (int)Speed; @end
//@interface Bus : Vehical @interface Bus : NSObject { int _seat; Vehical *_vehical; }
- (void)SetSeat:(int)a; - (int)Seat; - (void)SetVehicalWithWheels:(int)c andSpeed:(double)d; - (void)WheelsAndSpeed; @end
int main () { Bus *b = [Bus new];
[b SetVehicalWithWheels:12 andSpeed:50]; [b WheelsAndSpeed]; Vehical *v = [Vehical new]; [v SetWheels:100]; NSLog(@"%d",[v Wheels]);
return 0; }
@implementation Vehical
- (void)SetWheels:(int)a { _wheels = a; int b = 0; b++; NSLog(@"b = %d",b); } - (void)SetSpeed:(double)b { _speed = b; } - (int)Wheels { return _wheels; } - (int)Speed { return _speed; }
@end
@implementation Bus
- (void)SetSeat:(int)a { _seat = a; } - (int)Seat { return _seat; } - (void)SetVehicalWithWheels:(int)c andSpeed:(double)d { [_vehical SetWheels:c]; [_vehical SetSpeed:d]; } - (void)WheelsAndSpeed { int a = [_vehical Wheels]; int b = [_vehical Speed]; NSLog(@"Wheels:%d,Speed:%d",a,b); } @end |