oc_study04

来源:互联网 发布:山东软件协会 编辑:程序博客网 时间:2024/06/05 01:04

多重类的方法嵌套

typedef enum {    SexMan,    SexWoman} Sex;typedef struct {    int year;    int month;    int day;} Date;typedef enum {    ColorBlack,    ColorRed,    ColorGreen} Color;#import <Foundation/Foundation.h>@interface Dog : NSObject{@public    double weight;    Color curColor;}- (void)eat;- (void)run;@end@implementation Dog- (void)eat{    weight = weight + 1;    NSLog(@"狗狗吃完这餐后体重为%0.2lfKg", weight);}- (void)run{    weight = weight - 1;    NSLog(@"狗狗跑完步后体重为%0.2lfKg", weight);}@end@interface Student : NSObject{    @public    Sex sex;    Date birthday;    double weight;    Color favColor;    char * name;    Dog * dog;}- (void)eat;- (void)run;- (void)print;- (void)walkDog;- (void)feedDog;@end@implementation Student- (void)eat{    weight = weight + 1;    NSLog(@"学生吃完这餐后体重为%0.2lfKg", weight);}- (void)run{    weight = weight - 1;    NSLog(@"学生跑完步后体重为%0.2lfKg", weight);}- (void)print{    NSLog(@"性别 = %d, 喜欢的颜色 = %d, 姓名 = %s, 生日 = %d-%d-%d", sex, favColor, name, birthday.year, birthday.month, birthday.day);}- (void)feedDog{    [dog eat];}- (void)walkDog{    dog->weight = dog->weight - 1;    NSLog(@"学生遛完狗后狗的体重为%0.2lfKg", dog->weight);}@endint main(void){    Student * p = [Student new];    p->weight = 55.6789;        Date Birth = {1992,2,10};    p->birthday = Birth;        p->sex = SexMan;        p->favColor = ColorBlack;        p->name = "Jack";        [p eat];    [p run];    [p print];        p->dog = [Dog new];        p->dog->weight = 20;        [p->dog eat];    [p->dog run];        [p walkDog];    [p feedDog];        return 0;}


0 0
原创粉丝点击