/* //----------------------------------------
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!
|