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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© lumingfu 中级黑马   /  2015-1-30 13:40  /  953 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

学生  成员变量:性别、生日、体重、最喜欢的颜色、狗(体重、毛色、吃、跑)
         方法:吃、跑步、遛狗、喂狗
#import <Foundation/Founfation.h>
//枚举变量
typedef enum
{
     SexMan,
     SexWoman
}Sex;
typedef struct
{
     int year;
     int month;
     int day;
}Date;
typedef enum
{
     ColorBlack,
     ColorRed,
     ColorGreen
}Color;

@interface Dog:NSObject
{
     @public
     double weight;//体重
     Color curColor;//毛色  
}
- (void)eat;
- (void)run;
@end
@implement Dog
- (void)eat
{
     weight+=1;
     NSLog(@"狗吃完这次之后的体重是:%f",weight);
}
- (void)run
{
     weight -= 1;
     NSLog(@"狗跑完这次的体重是:%f",weight);
}
@end

@interface Student:NSObject
{
     @public
     Sex sex;//性别
     Date birthday;//生日
     double weight;//体重(kg)
     Color favColor;//最喜欢的颜色
     char name;
     Dog *dog;
}
- (void)eat;
- (void)run;
- (void)print;
- (void)liuDog;
- (void)weiDog;
@end

@implement Student
- (void)eat
{
     weight = weight + 1;//weight+=1;
     NSLog(@"学生吃完这次之后的体重是:%f",weight);
}
- (void)run
{
     weight -= 1;
     NSLog(@"学生跑完这次的体重是:%f",weight);
}
- (void)print
{
     NSLog(@"姓名:%s,性别:%d,体重:%d,喜欢的颜色:%d,生日:%d-%d-%d,",name,sex,weight,favColor,birthday.year,birthday.month,birthday.day);
}
- (void)liuDog
{
     //让狗跑起来,调用狗的run方法
     [dog run];
}
- (void)weiDog
{
     //让狗吃东西,调用狗的eat方法
     [dog eat];
}
@end
int main()
{
     Student *s = [Student new];
     Dog *d = [Dog new];
     d->curColor = ColorGreen;
     d->weight = 20;
     s->dog = [Dog new];
     s->name = "jack";
     //体重
     s->weight = 50;
     //性别
     s->sex = SexMan;
     //生日
     //s->birthday = {2011,2,23};不能这么写
     //s->birthday.year = 2011;
     //s->birthday.month = 2;
     //s->birthday.day = 23;
     Date d = {2011,2,23};
     s->birthday = d;
     //喜欢的颜色
     s->favColor = ColorBlack;
     [s eat];
     [s run];
     [s run];
     [s print];
     [s liuDog];
     [s weiDog];
     return 0;
}

0 个回复

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