A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

定义了一个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

5 个回复

倒序浏览
:( 是:  (
回复 使用道具 举报
输出结果是:
Wheels : 0,Speed : 0
b = 1
100
回复 使用道具 举报
因为内存中没有 Vehical , Bus 类 也没有接收 Vehical 对象的方法
解决办法:
1. 在 Bus 加上  - (void) setVehical:(Vehical *)vehical;
2. 在main函数中需要 创建一个 Vehical对象 , 然后set 给 Bus的vehical 成员变量, 这样就能有值了.
Vehical * vehical = [[Vehical alloc] init];
    [b setVehical:vehical];
3. 写法太奇怪 :L
回复 使用道具 举报
Vehical.h
  1. #import <Foundation/Foundation.h>

  2. @interface Vehical : NSObject
  3. {
  4.     int _wheels;
  5.     double _speed;
  6. }
  7. - (void)SetWheels:(int)a;
  8. - (void)SetSpeed:(double)b;
  9. - (int)Wheels;
  10. - (int)Speed;
  11. @end
复制代码

Vehical.m
  1. #import "Vehical.h"

  2. @implementation Vehical

  3. - (void)SetWheels:(int)a
  4. {
  5.     _wheels = a;
  6.     int b = 0;
  7.     b++;
  8.     NSLog(@"b = %d",b);
  9. }
  10. - (void)SetSpeed:(double)b
  11. {
  12.     _speed = b;
  13. }
  14. - (int)Wheels
  15. {
  16.     return _wheels;
  17. }
  18. - (int)Speed
  19. {
  20.     return _speed;
  21. }

  22. @end
复制代码

Bus.h
  1. #import <Foundation/Foundation.h>
  2. @class Vehical;
  3. @interface Bus : NSObject
  4. {
  5.     int _seat;
  6.     Vehical *_vehical;
  7. }

  8. - (void)SetSeat:(int)a;
  9. - (int)Seat;
  10. - (void)SetVehicalWithWheels:(int)c andSpeed:(double)d;
  11. - (void)WheelsAndSpeed;

  12. //=== 修改 ====================================
  13. - (void) setVehical:(Vehical *)vehical;

  14. @end
复制代码

Bus.m
  1. #import "Bus.h"
  2. #import "Vehical.h"
  3. @implementation Bus

  4. - (void)SetSeat:(int)a
  5. {
  6.         _seat = a;
  7. }
  8. - (int)Seat
  9. {
  10.         return _seat;
  11. }
  12. - (void)SetVehicalWithWheels:(int)c andSpeed:(double)d
  13. {
  14.         [_vehical SetWheels:c];
  15.         [_vehical SetSpeed:d];
  16. }
  17. - (void)WheelsAndSpeed
  18. {
  19.         int a = [_vehical Wheels];
  20.         int b = [_vehical Speed];
  21.         NSLog(@"Wheels:%d,Speed:%d", a, b);
  22. }

  23. //=== 修改 =====================
  24. - (void) setVehical:(Vehical *)vehical {
  25.     _vehical = vehical;
  26. }
  27. @end
复制代码

main.m
  1. #import <Foundation/Foundation.h>
  2. #import "Vehical.h"
  3. #import "Bus.h"

  4. int main()
  5. {
  6.         Bus * b = [Bus new];
  7.     //=== 修改 =====================
  8.     Vehical * vehical = [[Vehical alloc] init];
  9.     [b setVehical:vehical];
  10.     //===============================

  11.         [b SetVehicalWithWheels:12 andSpeed:50];
  12.         [b WheelsAndSpeed];

  13.         Vehical * v = [Vehical new];
  14.         [v SetWheels:100];
  15.         NSLog(@"%d", [v Wheels]);

  16.         return 0;
  17. }
复制代码
回复 使用道具 举报
谢谢!确实是内存中没有。  我在 - (void)SetVehicalWithWheels:(int)c andSpeed:(double)d 中添加了    Vehical *b = [Vehical new];
    _vehical = b;   内存有了,程序就通啦。btw你头像很漂亮了。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马