黑马程序员技术交流社区
标题: 求帮助,OC中类组合的问题。 [打印本页]
作者: XiaoBaoMi 时间: 2015-5-17 12:16
标题: 求帮助,OC中类组合的问题。
定义了一个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
作者: XiaoBaoMi 时间: 2015-5-17 12:18
:( 是: (
作者: XiaoBaoMi 时间: 2015-5-17 12:29
输出结果是:
Wheels : 0,Speed : 0
b = 1
100
作者: 香草芭芙 时间: 2015-5-17 13:20
因为内存中没有 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
作者: 香草芭芙 时间: 2015-5-17 13:22
Vehical.h
- #import <Foundation/Foundation.h>
- @interface Vehical : NSObject
- {
- int _wheels;
- double _speed;
- }
- - (void)SetWheels:(int)a;
- - (void)SetSpeed:(double)b;
- - (int)Wheels;
- - (int)Speed;
- @end
复制代码
Vehical.m
- #import "Vehical.h"
- @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
复制代码
Bus.h
- #import <Foundation/Foundation.h>
- @class Vehical;
- @interface Bus : NSObject
- {
- int _seat;
- Vehical *_vehical;
- }
- - (void)SetSeat:(int)a;
- - (int)Seat;
- - (void)SetVehicalWithWheels:(int)c andSpeed:(double)d;
- - (void)WheelsAndSpeed;
- //=== 修改 ====================================
- - (void) setVehical:(Vehical *)vehical;
- @end
复制代码
Bus.m
- #import "Bus.h"
- #import "Vehical.h"
- @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);
- }
- //=== 修改 =====================
- - (void) setVehical:(Vehical *)vehical {
- _vehical = vehical;
- }
- @end
复制代码
main.m
- #import <Foundation/Foundation.h>
- #import "Vehical.h"
- #import "Bus.h"
- int main()
- {
- Bus * b = [Bus new];
- //=== 修改 =====================
- Vehical * vehical = [[Vehical alloc] init];
- [b setVehical:vehical];
- //===============================
- [b SetVehicalWithWheels:12 andSpeed:50];
- [b WheelsAndSpeed];
- Vehical * v = [Vehical new];
- [v SetWheels:100];
- NSLog(@"%d", [v Wheels]);
- return 0;
- }
复制代码
作者: XiaoBaoMi 时间: 2015-5-17 13:55
谢谢!确实是内存中没有。 我在 - (void)SetVehicalWithWheels:(int)c andSpeed:(double)d 中添加了 Vehical *b = [Vehical new];
_vehical = b; 内存有了,程序就通啦。btw你头像很漂亮了。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) |
黑马程序员IT技术论坛 X3.2 |