黑马程序员_类的合理设计_基本的属性&对象类型的属性
来源:互联网 发布:南风知我意七微书包网1 编辑:程序博客网 时间:2024/05/23 18:52
黑马程序员_类的合理设计_基本的属性&对象类型的属性
一、类的合理设计之基本的属性
类的合理设计1_基本的属性/* 学生 成员变量:性别、生日、体重、最喜欢的颜色、狗 方法: 吃、跑步、遛狗、喂狗 */#import <Foundation/Foundation.h>typedef enum {SexMan,SexWoman} Sex;typedef struct {int year;int month;int day;} Date;typedef enum {ColorBlack;ColorRed;ColorGreen} Color;@interface Student : NSObject{@publicSex sex;// 性别Date birthday;// 生日double weight;// 体重(kg)Color favColor;// 最喜欢的颜色char *name;}- (void)eat;- (void)run;- (void)print;@end@implementation Student- (void)eat{weight += 1;// 每吃一次,体重就加1NSLog (@"吃完这次后的体重是%f", weight);}- (void)run{weight -= 1;// 跑一次,体重减1NSLog (@"跑完这次后的体重是%f", weight);}- (void)print{NSLog (@"性别= %d, 喜欢的颜色 = %d, 姓名 = %s, 生日 = %d-%d-%d",sex, favcolor, name, birthday.year, birthday.month, birthday.day);}@endint main(){Student *s = [Student new];s->weight = 50;// 性别s->sex = SexMan;// 生日/*// s->birthday = {2011, 9, 10};// 在定义结构体变量的时候才可以初始化。s->birthday.year = 2011;s->birthday.month = 9;s->birthday.day = 10; */Date d = {2011, 9, 10};s->birthday = d;// 姓名s->name = "Jack";// 颜色s->favColor = ColorBlack;[s print];return 0;}
二、类的合理设计之对象类型的属性
类的合理设计2_对象类型的属性/* 学生 成员变量:性别、生日、体重、最喜欢的颜色、狗(体重、毛色、吃、跑) 方法: 吃、跑步、遛狗(让狗跑)、喂狗(让狗吃) */#import <Foundation/Foundation.h>typedef enum {SexMan,SexWoman} Sex;typedef struct {int year;int month;int day;} Date;typedef enum {ColorBlack;ColorRed;ColorGreen} Color;@interface Dog : NSObject{@publicdouble weight;// 体重Color curColor;// 毛色}- (void)eat;- (void)run;@end@implementation Dog- (void)eat{weight += 1;// 每吃一次,体重就加1NSLog (@"狗吃完这次后的体重是%f", weight);}- (void)run{weight -= 1;// 跑一次,体重减1NSLog (@"狗跑完这次后的体重是%f", weight);}@end@interface Student : NSObject{@publicSex sex;// 性别Date birthday;// 生日double weight;// 体重(kg)Color favColor;// 最喜欢的颜色char *name;// 重点:狗Dog *dog;}- (void)eat;- (void)run;- (void)print;- (void)liuDog;- (void)weiDog;@end@implementation Student- (void)liuDog{// 让狗跑起来(调用狗的run方法)[dog run];}- (void)weiDog{// 让狗吃东西(调用狗的eat方法)[dog eat];}- (void)eat{weight += 1;// 每吃一次,体重就加1NSLog (@"学生吃完这次后的体重是%f", weight);}- (void)run{weight -= 1;// 跑一次,体重减1NSLog (@"学生跑完这次后的体重是%f", weight);}- (void)print{NSLog (@"性别= %d, 喜欢的颜色 = %d, 姓名 = %s, 生日 = %d-%d-%d",sex, favcolor, name, birthday.year, birthday.month, birthday.day);}@endint main(){Student *s = [Student new];Dog *d = [Dog new];d->curColor = ColorGreen;d->weight = 20;// 创建一个Dog对象给Student对象s->dog = d;[s liuDog];[s weiDog];return 0;}void test(){Student *s = [Student new];s->weight = 50;// 性别s->sex = SexMan;// 生日/*// s->birthday = {2011, 9, 10};// 在定义结构体变量的时候才可以初始化。s->birthday.year = 2011;s->birthday.month = 9;s->birthday.day = 10; */Date d = {2011, 9, 10};s->birthday = d;// 姓名s->name = "Jack";// 颜色s->favColor = ColorBlack;[s print];}
0 0
- 黑马程序员_类的合理设计_基本的属性&对象类型的属性
- 黑马程序员_数组的length属性
- [黑马程序员]_面向对象的概念、对象引用及属性
- [黑马程序员]_面向对象的概念、对象引用及属性
- 黑马程序员 C#基础_属性与字段的区别
- 黑马程序员_面向对象,类,方法,属性
- 黑马程序员_对象、类、属性、访问修饰符
- 黑马程序员_面向对象的情景分析与设计
- 黑马程序员-类的合理设计
- 黑马程序员_字符串的基本操作方法
- 黑马程序员_代理的基本实现
- document对象_标签的属性
- 1.Python深入_对象的属性
- 黑马程序员_面向对象的应用
- 黑马程序员_面向对象的思想
- 黑马程序员_面对对象的思想
- 黑马程序员_面向对象的特性
- 黑马程序员_对象的序列化
- Unable to compile class for JSP 错误的解决过程
- bug 非Activity中使用startActivity
- pkg_info 命令使用方法
- js+jquery动态设置/增加/删除/获取元素属性的两种方法集锦对比(动态onclick属性设置+动态title设置)
- Memcache客户端,随意写的
- 黑马程序员_类的合理设计_基本的属性&对象类型的属性
- 【转】Myeclipse 10.6 导出war问题解决思路
- OC学习4——@property和@synthesize
- Qt学习之将资源文件添加到工程里面
- linux unlink() access() mkfifo()使用实例
- 时间类
- linux下top命令的使用
- 嵌入式Linux启动过程
- QT中文乱码问题