iOS之03-类的合理设计

以下代码为了充分学习理解 类与对象

类与对象的定义 

  类就是将事物的共有属性和方法抽离出来形成的;类是现实世界或思维世界中的实体在计算机中的反映,它将数据以及这些数据上的操作封装在一起。

  对象是具有类类型的变量。类和对象是面向对象编程技术中的最基本的概念。

类对象的关系

  类是对象的抽象,而对象是类的具体实例。类是抽象的,不占用内存,而对象是具体的,占用存储空间。类是用于创建对象的蓝图,它是一个定义包括在特定类型的对象中的方法和变量的软件模板。
  1 /*
  2 学生
  3  成员变量:性别、生日、体重、最喜欢的颜色、养的狗(体重、毛色、吃、跑)
  4  方法:吃、跑步、遛狗、喂狗
  5  
  6  */
  7 
  8 #import <Foundation/Foundation.h>
  9 
 10 // 性别枚举
 11 typedef enum {// 枚举类型常量一般是以枚举名开头(规范)
 12     SexMan,  //0
 13     SexWoman //1
 14 } Sex;
 15 
 16 // 生日结构体
 17 typedef struct {
 18     int year;
 19     int month;
 20     int day;
 21 } Date;
 22 
 23 // 颜色结构体
 24 typedef enum {
 25     ColorBlack,//0
 26     ColorRed,  //1
 27     ColorGreen //2
 28 } Color;
 29 
 30 // Dog类的声明
 31 @interface Dog : NSObject
 32 {// 成员变量
 33     @public
 34     double weight; //体重
 35     Color curColor;//毛色
 36 }
 37 // 方法声明
 38 - (void)eat;
 39 - (void)run;
 40 @end
 41 
 42 // Dog类的实现
 43 @implementation Dog
 44 // 狗吃东西的方法
 45 - (void)eat {
 46     weight++;
 47     NSLog(@"吃完这次后的体重是%.2f", weight);
 48 }
 49 // 狗跑的方法
 50 - (void)run {
 51     weight--;
 52     NSLog(@"跑完这次后的体重是%.2f", weight);
 53 }
 54 @end
 55 
 56 // Student类的声明
 57 @interface Student : NSObject
 58 {
 59     @public
 60     char *name; // 名字
 61     Sex sex; // 性别
 62     Date birthday; // 生日
 63     double weight; // 体重(kg)
 64     Color favColor; // 最喜欢的颜色
 65     Dog *dog; // 养的狗
 66 }
 67 // 人吃东西的方法声明
 68 - (void)eat;
 69 // 人跑步的方法声明
 70 - (void)run;
 71 // 输出人的信息方法声明
 72 - (void)print;
 73 // 人遛狗方法声明
 74 - (void)liuDog;
 75 // 人喂狗方法声明
 76 - (void)feed;
 77 @end
 78 
 79 // Student类的实现
 80 @implementation Student
 81 
 82 // 人吃东西的方法实现
 83 - (void)eat {
 84     weight++;
 85     NSLog(@"狗吃完这次后的体重是%.2f", weight);
 86 }
 87 
 88 // 人跑步的方法实现
 89 - (void)run {
 90     weight--;
 91     NSLog(@"狗跑完这次后的体重是%.2f", weight);
 92 }
 93 
 94 // 输出人的信息方法实现
 95 - (void)print {
 96     NSLog(@"名字 = %s,性别 = %d,生日 = %d-%d-%d,体重 = %.2f,喜欢的颜色 = %d", name, sex, birthday.year, birthday.month, birthday.day, weight, favColor);
 97 }
 98 
 99 // 人遛狗方法实现
100 - (void)liuDog {
101     // 让狗跑起来(调用狗的run方法)
102     [dog run];
103 }
104 
105 // 人喂狗方法实现
106 - (void)feed {
107     // 让狗吃东西(调用狗的eat方法)
108     [dog eat];
109 }
110 @end
111 
112 int main() {
113 
114     // 创建一个学生对象
115     Student *s = [Student new];
116     
117     // 人的属性赋值
118     s->name = "Jack";
119     s->weight = 50;
120     s->sex = SexMan;
121     // 生日;
122     /*
123      s->birthday.year = 2011;
124      s->birthday.month = 9;
125      s->birthday.day = 10;
126      */
127     Date d = {2011, 9, 10};
128     s->birthday = d;
129     // 喜欢的颜色
130     s->favColor = ColorRed;
131     
132     // 人的方法调用
133     [s eat];
134     [s eat];
135     [s run];
136     [s run];
137     [s print];
138     
139     // 创建一个狗的对象
140     Dog *dog = [Dog new];
141     
142     // 狗的属性赋值
143     dog->weight = 20;
144     dog->curColor = ColorBlack;
145     
146     // 将狗对象赋值给人的 狗属性
147     s->dog = dog;
148     
149     // 人的方法调用
150     [s liuDog];
151     [s feed];
152     
153     return 0;
154 }
posted @ 2016-03-12 21:11  墨隐于非  阅读(289)  评论(0编辑  收藏  举报