- #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;
- }
复制代码
|
|