黑马程序员技术交流社区
标题:
继承和组合,对象调用
[打印本页]
作者:
xileo0
时间:
2015-3-8 12:08
标题:
继承和组合,对象调用
#import <Foundation/Foundation.h>
//身材
@interface Stature : NSObject
{
double _height;//身高 /m
double _weight;//体重 /kg
}
- (void)setHeight:(double)height;
- (double)height;
- (void)setWeight:(double)weight;
- (double)weight;
@end
@implementation Stature
// 身高的getter和setter
- (void)setHeight:(double)height
{
_height = height;
}
- (double)height
{
return _height;
}
// 体重的getter和setter
- (void)setWeight:(double)weight
{
_weight = weight;
}
- (double)weight
{
return _weight;
}
@end
@interface Person : NSObject
{
Stature *_stature;//身材
int _age;//年龄
}
- (void)setAge:(int)age;
- (int)age;
//---身材的setter和getter
- (void)setStature:(Stature *)stature;
- (Stature *)stature;
@end
@implementation Person
- (void)setAge:(int)age
{
_age = age;
}
- (int)age
{
return _age;
}
- (void)setStature:(Stature *)stature
{
_stature = stature;
}
- (Stature *)stature
{
return _stature;
}
@end
int main()
{
Person *p = [Person new];
[p setAge:24];//p.age
Stature *s = [Stature new];
[s setHeight:1.8];
[s setWeight:60];
[p setStature:s];
return 0;
}
复制代码
作者:
xileo0
时间:
2015-3-8 12:09
额。。问题不见了。要输出p对象的身材成员值,要怎样写输出语句,怎样调用组合的成员变量值
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2