黑马程序员技术交流社区
标题: OC编程中类的组合问题 [打印本页]
作者: Lxy 时间: 2014-10-18 17:53
标题: OC编程中类的组合问题
/*
//----------------------------------------
2.设计2个类,类之间的关系自拟(比如继承、组合)
1> 身材数据
(1)属性
* 身高
* 体重
* 手长
* 脚长
(2)方法
* 属性相应的set和get方法
2> 人
(1)属性
* 年龄
* 身高
* 体重
* 手长
* 脚长
(2)方法
* 属性相应的set和get方法
*/
#import <Foundation/Foundation.h>
@interface Body : NSObject
{
double _height;
double _weight;
double _hands;
double _foots;
}
- (void)setHeight:(double)height;
- (double)height;
- (void)setWeight:(double)Weight;
- (double)weight;
- (void)setHands:(double)hands;
- (double)hands;
- (void)setFoots:(double)foots;
- (double)foots;
@end
@implementation Body
- (void)setHeight:(double)height
{
self->_height = height;
}
- (double)height
{
return self->_height;
}
- (void)setWeight:(double)weight
{
self->_weight = weight;
}
- (double)weight
{
return self->_weight;
}
- (void)setHands:(double)hands
{
self->_hands = hands;
}
- (double)hands
{
return self->_hands;
}
- (void)setFoots:(double)foots
{
self->_foots = foots;
}
- (double)foots
{
return self->_foots;
}
@end
@interface Person : NSObject
{
int _age;
Body * _body;
}
- (void)setAge:(int)age;
- (int)age;
- (void)setBodyofHeight:(double)height ofWeight:(double)weight ofHands:(double)hands ofFoots:(double)foots;
- (Body *)body ;
@end
@implementation Person
- (void)setAge:(int)age
{
self->_age = age;
}
- (int)age
{
return self->_age;
}
- (void)setBodyofHeight:(double)height ofWeight:(double)weight ofHands:(double)hands ofFoots:(double)foots
{
//_body = [body setHeight:height];
[self->_body setHeight:height];
[self->_body setWeight:weight];
[self->_body setHands:hands];
[self->_body setFoots:foots];
}
- (Body *)body
{
return self->_body;
}
@end
int main()
{
Person * p = [Person new];
[p setBodyofHeight:173.5 ofWeight:58.0 ofHands:13.2 ofFoots:14.3];
Body * b = [p body];
NSLog(@"b->_height=%f,b->_weight=%f,b->_hands=%f,b->_foots=%f",[b height],[b weight], [b hands], [b foots]);
return 0;
}
b->_height=0.000000,b->_weight=0.000000,b->_hands=0.000000,b->_foots=0.000000
为毛输出结果都为0!
作者: Lxy 时间: 2014-10-18 17:55
求大师指点!!
作者: 弹琴骚年 时间: 2014-10-18 22:26
你Person类里的属性_body这个对象在你创建Person对象的时候_body里面的值是空的,即nil。
之后你再_body的set方法里用[self->_body setHeight:height];,这是没有意义的。相当于[nil setHeight:height];
你应该在_body的set方法里先创建一个Body对象,再把对象赋给_body。
代码如下:
- - (void)setBodyofHeight:(double)height ofWeight:(double)weight ofHands:(double)hands ofFoots:(double)foots
- {
- Body *b = [Body new];
- [b setHeight:height];
- [b setWeight:weight];
- [b setHands:hands];
- [b setFoots:foots];
- self->_body = b;
- }
复制代码
作者: Lxy 时间: 2014-10-19 00:54
嗯,已经解决了!方法的架构不好,要加个形参(Body *)body
作者: 崔石炫 时间: 2014-10-19 15:40
加油ahhhhhhhhhhhhh……
作者: Lxy 时间: 2014-10-19 15:57
上面代码的写法,还是没有转到面向对象,set设置方法形参应该只设置,要调用该方法的对象的属性,即可!用到的各个类对象,在主函数main中创建并初始化,而不是全部集中在一个方法中,就像上面的
- (void)setBodyofHeight:(double)height ofWeight:(double)weight ofHands:(double)hands ofFoots:(double)foots
{
//_body = [body setHeight:height];
[self->_body setHeight:height];
[self->_body setWeight:weight];
[self->_body setHands:hands];
[self->_body setFoots:foots];
}
应该是
- (void)setBodyofHeight:(Body*)body
{
_body = body;
}
作者: duzhong 时间: 2014-10-19 22:08
好长的一串代码,可惜没装苹果系统,运行不了。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) |
黑马程序员IT技术论坛 X3.2 |