/*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 Faimaly : NSObject{
@public
Person *dad;
Person *mom;
Person *son;
}
-(void) printAll;
@end
@implementation Faimaly
-(void) printAll{
[dad printMe];////////疑问?dad p->dad
[mom printMe];///////mom
[son printMe];//////son
}
@end
int main(){
Person *p=[Person new];//创建一个人类的对象
p->name="jack";
p->birthDay.year=2015;
p->birthDay.month=5;
p->birthDay.day=18;
p->sex=MAN;//以上都是赋值
//[p talk:"haha"];
// [p smile];
//[p printMe];//以上都是调用方法
Faimaly *f=[Faimaly new];//创建一个家庭的对象
f->dad=p;
//[f->dad printMe];
Person *m=[Person new];//创建一个人类的对象母亲
m->name="lucy";
m->birthDay.year=2015;
m->birthDay.month=6;
m->birthDay.day=18;
m->sex=WOMAN;//以上都是赋值
f->mom=m;
Person *s=[Person new];//创建一个人类的对象孩子
s->name="malei";
s->birthDay.year=2015;
s->birthDay.month=7;
s->birthDay.day=18;
s->sex=MAN;//以上都是赋值
f->son=s;
[f printAll];
return 0;
}
|