/* 类的合理设计练习 需求:1.设计一个人类,每个人都有姓名,生日,性别,每个人都有说话与笑的方法. 2.设计一个家庭类有三个成员,父亲,母亲,孩子. */ #import <Foundation/Foundation.h> typedef struct{ int year; int month; int day; }Date; typedef enum { MAN, WOMAN,
}SEX; @interface Person:NSObject{ @public char *name; Date birthday; SEX sex; } -(void)talk:(char*)s; -(void)smile; -(void)printMe; @end @implementation Person -(void)talk:(char*)s{ NSLog(@"%s讲了一句话:%s",name,s); } -(void)smile{ NSLog(@"%s笑了",name); } -(void)printMe{ NSLog(@"姓名是:%s,生日是:%d-%d-%d,性别是:%d",name,birthday.year,birthday.month,birthday.day,sex); } @end
@interface Family : NSObject{ @public Person *dad; Person *mom; Person *child; } -(void)printAll; @end
@implementation Family -(void)printAll{ [dad printMe];//dad调用printMe方法 [mom printMe]; [child printMe]; }
@end int main(){ Family *f =[Family new];//创建一个家庭对象f Person *baba=[Person new];//创建一个家庭对象 dad f->dad=baba;//把baba对象设置给f对象内的dad属性. NSLog(@"%p",f->dad); NSLog(@"%p",f->mom); NSLog(@"%p",f->child); return 0; }
|