#import <Foundation/Foundation.h> typedef enum {GenderMan, GenderFemale} Gender; typedef enum {ColorRed, ColorBlue, ColorGreen} Color; typedef struct
{
int year;
int month;
int day;
} Date; @interface Dog : NSObject
{
@public
float weight;
Color color;
}
- (void) eat;
- (void) run; @end @interface Student : NSObject
{
@public
char *name;
Gender gender;
Date birthday;
double weight;
Color favariteColor;
Dog *dog;
} - (void) eat;
- (void) run;
- (void) walkDog;
- (void) feedDog;
- (void) print; @end @implementation Student
- (void) eat
{
weight++;
NSLog(@"吃吃吃,体重增加了1KG, 现在体重是%f", weight);
} - (void) run
{
weight--;
NSLog(@"跑跑跑,体重减去了1KG,现在体重是%f", weight);
} - (void) print
{
NSLog(@"这个学生的资料-》姓名:%s, 性别:%d, 生日:%d-%d-%d, 体重:%f, 喜爱的颜色:%d", name, gender, birthday.year, birthday.month, birthday.day, weight, favariteColor);
} - (void) walkDog
{
[dog run];
} - (void) feedDog
{
[dog eat];
}
@end @implementation Dog
- (void) eat
{
NSLog(@"喂狗啦!!!");
weight++;
NSLog(@"狗狗吃吃吃,体重增加了1KG, 现在体重是%f", weight);
} - (void) run
{
NSLog(@"遛狗啦!!!!");
weight--;
NSLog(@"狗狗跑跑跑,体重减去了1KG,现在体重是%f", weight);
}
@end int main()
{
Student *stu = [Student new];
stu->name = "Jack";
stu->gender = GenderMan;
Date d = {, , };
stu->birthday = d;
stu->weight = ;
stu->favariteColor = ColorRed; Dog *dog = [Dog new];
stu->dog = dog; [stu eat];
[stu feedDog];
[stu print];
[stu walkDog];
[stu feedDog]; return ;
}