年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2166|回复: 0

一个典型的例子说明面向对象的基本设计

[复制链接]
  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

    发表于 2014-3-10 17:08:20 | 显示全部楼层 |阅读模式
    本帖最后由 Sian 于 2014-3-10 20:46 编辑

    设计一个学生的类,要求该学生具备:1、属性:
          姓名;
          出生日期;
          性别;
          喜好的颜色;
          体重;
          狗:属性(体重、毛色)、方法(吃、跑);
    2、方法:
          吃;
          跑;
          遛狗([狗  跑]);
          喂狗([狗  吃]);


    源代码:
    1. #import <Foundation/Foundation.h>
    2. // 定义一个结构体类型 Date 用来定义日期类型数据
    3. typedef struct {
    4.     int year;
    5.     int month;
    6.     int day;
    7. } Date;
    8. // 定义一个枚举类型 Sex 用来定义性别类型数据
    9. typedef enum {
    10.     SexMan,
    11.     SexWoman
    12. } Sex;
    13. // 定义一个枚举类型 Color 用来定义颜色类型数据
    14. typedef enum {
    15.     ColorBlack,
    16.     ColorWrite,
    17.     ColorRed,
    18.     ColorBlue,
    19.     ColorGreen
    20. } Color;
    21. // 创建一个Dog对象
    22. @interface Dog : NSObject {  // 对象声明
    23. @public  // 成员变量属性设置为public,便于直接对成员变量进行访问,默认为protocted无法直接访问
    24.     double height;  // 成员变量height表示身高
    25.     Color color;  // 成员变量color表示毛色
    26. }
    27. - (void) eat;  // 声明方法:吃
    28. - (void) bray;  // 声明方法:叫
    29. - (void) run;  // 声明方法:跑
    30. - (void) print;  // 声明方法:打印各成员变量值
    31. @end
    32. @implementation Dog  // 方法实现
    33. // eat方法使体重增加1,并输出当前体重值
    34. - (void) eat{
    35.     height += 1;
    36.     NSLog(@"dog is eating...height=%.2f", height);
    37. }
    38. // bray方法,输出一行字符串表示
    39. - (void) bray {
    40.     NSLog(@"汪……汪……");
    41. }
    42. // run方法使得体重减少1,并输出当前体重
    43. - (void)run {
    44.     height -= 1;
    45.     NSLog(@"dog is runnig! height=%.2f", height);
    46. }
    47. // print方法输出当前成员变量值
    48. - (void)print {
    49.     NSLog(@"%@: height = %.2f, color = %d", self, height, color);
    50. }
    51. @end
    52. // 创建一个人的对象
    53. @interface Person : NSObject {  // 对象声明
    54. @public  // 成员变量属性设置为public,便于直接对成员变量进行访问,默认为protocted无法直接访问
    55.     char *name;  // 成员变量name,表示姓名
    56.     Date birthday;  // 成员变量 birthday表示出生年月日
    57.     Sex sex;  // 成员变量sex 表示性别
    58.     Color favColor;  // 成员变量favColor表示个人喜好的颜色
    59.     double height;  // 成员变量height表示体重
    60.     Dog *dog;  // 成员变量dog表示拥有的狗对象
    61. }
    62. // 声明对象方法
    63. - (void)eat;  // eat表示吃
    64. - (void)run;  // run表示跑
    65. - (void)heyDog;  // heyDog表示喂狗
    66. - (void)walkDog;  // walkDog表示遛狗
    67. - (void)print;  // print方法用来输出当前对象的成员变量值
    68. @end
    69. @implementation Person  // 方法实现
    70. // eat方法将成员变量height的值加1,并输出当前体重值
    71. - (void)eat {
    72.     height += 1;
    73.     NSLog(@"Person is running...height=%.2f", height);
    74. }
    75. // run方法将成员变量height的值减1,并输了当前体重值
    76. - (void)run {
    77.     height -= 1;
    78.     NSLog(@"Person is eating...height=%.2f", height);
    79. }
    80. // keyDog方法表示当前正在喂狗,同时调用狗的eat方法,即狗的体重增加1
    81. - (void)heyDog {
    82.     NSLog(@"Person hey the dog...");
    83.     [dog eat];
    84. }
    85. // walkDog方法表示当前正在遛狗,同时调用狗的run方法,即狗的体重减1
    86. - (void)walkDog {
    87.     NSLog(@"Person walk the dog...");
    88.     [dog run];
    89.     [dog bray];
    90. }
    91. // print方法输出对象所有成员变量的值
    92. - (void)print {
    93.     NSLog(@"%@: name=%s, birthday=(%d-%d-%d), sex=%d, favColor=%d, height=%.2f, dog=%@", self, name, birthday.year, birthday.month, birthday.day, sex, favColor, height, dog);
    94. }
    95. @end
    96. int main() {
    97.    
    98.     Person *p = [Person new];  // 创建一个Person对象p
    99.    
    100.     // 给各成员变量赋值
    101.     p->name ="yusian";
    102.    
    103.     p->sex = SexMan;
    104.    
    105.     Date d = {1987,3,21}; // 创建中间临时值,方便一次性赋值
    106.     p->birthday = d;
    107.    
    108.     p->favColor = ColorBlue;
    109.    
    110.     p->height = 72.4;
    111.    
    112.     p->dog = [Dog new];  // 创建一个Dog对象,并将对象赋值给当前Person对象
    113.    
    114.     // 给Dog对象各成员变量赋值
    115.     p->dog->height = 13.2;
    116.     p->dog->color = ColorBlue;
    117.    
    118.     // 输出对象p的各成员变量值
    119.     [p print];
    120.    
    121.     // 输出Dog对象的各成员变量值
    122.     [p->dog print];
    123.    
    124.     // 人跑步
    125.     [p run];
    126.    
    127.     // 人吃饭
    128.     [p eat];
    129.    
    130.     // 人喂狗
    131.     [p heyDog];
    132.    
    133.     // 人遛狗
    134.     [p walkDog];
    135.     return 0;
    136. }
    复制代码
    输出结果:
    1. 2014-03-10 20:45:46.744 a.out[1404:507] <Person: 0x7fd86340ad00>: name=yusian, birthday=(1987-3-21), sex=0, favColor=3, height=72.40, dog=<Dog: 0x7fd863408f40>
    2. 2014-03-10 20:45:46.746 a.out[1404:507] <Dog: 0x7fd863408f40>: height = 13.20, color = 3
    3. 2014-03-10 20:45:46.746 a.out[1404:507] Person is eating...height=71.40
    4. 2014-03-10 20:45:46.746 a.out[1404:507] Person is running...height=72.40
    5. 2014-03-10 20:45:46.747 a.out[1404:507] Person hey the dog...
    6. 2014-03-10 20:45:46.747 a.out[1404:507] dog is eating...height=14.20
    7. 2014-03-10 20:45:46.747 a.out[1404:507] Person walk the dog...
    8. 2014-03-10 20:45:46.748 a.out[1404:507] dog is runnig! height=13.20
    9. 2014-03-10 20:45:46.748 a.out[1404:507] 汪……汪……
    复制代码




    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-5-5 22:21 , Processed in 0.047834 second(s), 23 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表