A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

#import <Foundation/Foundation.h>
//要求:建立一个类包涵复杂类型成员变量,类里包涵一个类
//建立一个Dog类,包含了枚举类型
typedef enum{
    MaoSeBlack,
    MaoSeWrith
} MaoSe;

@interface Dog : NSObject
{
    @public
    int weight;
    MaoSe maoSe;
}
- (void)run;
- (void)eat;
@end

@implementation Dog
- (void)run{
    weight--;
    NSLog(@"狗跑了一会儿后体重%dkg", weight);
}
- (void)eat{
    weight++;
    NSLog(@"狗吃了一次食物后体重%dkg", weight);
}
@end
//**************************************************//
//建立一个Student类,包含Dog类和枚举类型和结构体类型
typedef struct{
    int year;
    int mouth;
    int day;
} Brithday;

typedef enum{
    SexMan,
    SexWoman
} Sex;

@interface Student : NSObject
{
    @public
    int age;
    int weight;
    Brithday date;
    Sex sex;
    Dog *dog;
}
- (void)print;
- (void)run;
- (void)eat;
- (void)liuGou;
- (void)weiGou;
@end

@implementation Student
- (void)print{
    NSLog(@"学生性别%d,年龄%d岁,体重%d公斤,生日%d年%d月%d日,宠物狗的毛色%d,狗的体重%d公斤", sex, age, weight, date.year, date.mouth, date.day, dog->maoSe, dog->weight);
}
- (void)run{
    weight--;
    NSLog(@"学生跑了一段时间体重为%dkg", weight);
}
- (void)eat{
    weight++;
    NSLog(@"学生吃了一顿饭为%dkg", weight);
}
- (void)liuGou{
    [dog run];
}
- (void)weiGou{
    [dog eat];
}
@end
//****************************************************//
int main(){
    Student *s = [Student new];
//    用dog变量时,先建立一个Dog类的一个新的dog对象
    s->dog = [Dog new];
    s->age = 17;
    s->sex = SexMan;
    s->weight = 40;
//    结构体变量在这里不能直接赋值
    Brithday b = {1987, 9, 5};
    s->date = b;
    s->dog->weight = 10;
    s->dog->maoSe = MaoSeWrith;
    [s print];
    [s run];
    [s eat];
    [s liuGou];
    [s weiGou];
    return 0;
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马