类的合理设计
来源:互联网 发布:sql语句删除列 编辑:程序博客网 时间:2024/05/21 11:00
#import <Foundation/Foundation.h>typedef enum {//枚举 SexMan, SexWoman}Sex;typedef struct { int year; int month; int day;}Data;typedef enum { ColorBlack, ColorRed, ColorGreen}Color;@interface Dog : NSObject{ @public double weight; //体重 Color curColor; //毛色}- (void)run;- (void)eat;@end@implementation Dog- (void)eat{ //每吃一次,体重加1 weight += 1; NSLog(@"狗吃完这次后的体重是%.2f",weight);}- (void)run{ weight -= 1; NSLog(@"狗跑完这次后的体重是%.2f",weight);}@end/*学生成员变量:性别、生日、体重、最喜欢的颜色、狗(体重、毛色、吃、跑)方法:吃、跑步、遛狗(让狗跑)、喂狗(让狗吃)、*/@interface Student : NSObject{ @public Sex sex; //性别 Data birthday; //生日 double weight; //体重(kg) Color favColor; //最喜欢的颜色 char *name; //重点 Dog *dog;//任何OC对象肯定是用指针来指的 }- (void)eat;- (void)run;- (void)print;- (void)liuDog;- (void)weiDog;@end@implementation Student- (void)liuDog{ //让狗跑起来(调用狗的run方法) [dog run];}- (void)weiDog{ //让狗吃东西(调用狗的eat方法) [dog eat];}- (void)print{ NSLog(@"性别=%d,喜欢的颜色=%d,姓名=%s,生日=%d-%d-%d",sex,favColor,name,birthday.year,birthday.month,birthday.day);}- (void)eat{ //每吃一次,体重加1 weight += 1; NSLog(@"学生吃完这次后的体重是%.2f",weight);}- (void)run{ weight -= 1; NSLog(@"学生跑完这次后的体重是%.2f",weight);}@endint main(){ Student *s = [Student new]; Dog *sdog = [Dog new]; sdog->curColor = ColorGreen; sdog->weight = 20; s->dog = sdog;//对象中又有对象 [s liuDog]; [s weiDog]; s->weight = 50; //性别 s->sex = SexMan; //生日 //s->birthday = {2011, 9, 10};错误 Data d ={1993 , 9 , 9}; s->birthday = d; /* s->birthday.year = 1993; s->birthday.month = 9; s->birthday.day = 9; */ //姓名 s->name = "DaShu"; //喜欢的颜色 s->favColor = ColorBlack; [s eat]; [s eat]; [s run]; [s run]; [s print]; return 0;}
0 0
- 类的合理设计
- 类的合理设计
- 关于类的合理设计
- 黑马程序员-类的合理设计
- iOS之03-类的合理设计
- 合理的网页设计
- SQL2005合理的索引设计
- SQL2005合理的索引设计
- SQL2005合理的索引设计
- SQL2005合理的索引设计
- SQL2005合理的索引设计
- 三、类和对象~-类的合理设计
- IOS开发---OC语言-④类的合理设计
- iOS类的合理设计,面向对象思想
- 合理设计
- 设计高效合理的MySQL查询语句
- 设计高效合理的MySQL查询语句
- 09-自定义filter_合理的设计方式
- The new features of OSX 10.11 and Xcode 7
- Set接口
- [leetcode 205] Isomorphic Strings
- 使用bootStrap-validator
- JVM中进程的工作目录
- 类的合理设计
- DFS入门:全排列算法及POJ 1564 Sum it up详解
- 制作initrd(1):向initrd内部更新驱动模块
- 越来越觉得自己啥都不会
- 方法的声明和实现
- Java字符串String中contains与indexOf的区别
- Python基础_scrapy安装心得
- java取得当前工作目录
- 【解决方法】安装ubuntu后,ubuntu与Windows7双系统,只能进入ubuntu不能进入win7