黑马程序员_类的合理设计_基本的属性&对象类型的属性
2014-06-11 17:21
246 查看
黑马程序员_类的合理设计_基本的属性&对象类型的属性
一、类的合理设计之基本的属性
二、类的合理设计之对象类型的属性
类的合理设计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
{
@public
double weight; // 体重
Color curColor; // 毛色
}
- (void)eat;
- (void)run;
@end
@implementation Dog
- (void)eat
{
weight += 1; // 每吃一次,体重就加1
NSLog (@"狗吃完这次后的体重是%f", weight);
}
- (void)run
{
weight -= 1; // 跑一次,体重减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
@implementation Student
- (void)liuDog
{
// 让狗跑起来(调用狗的run方法)
[dog run];
}
- (void)weiDog
{
// 让狗吃东西(调用狗的eat方法)
[dog eat];
}
- (void)eat
{
weight += 1; // 每吃一次,体重就加1
NSLog (@"学生吃完这次后的体重是%f", weight);
}
- (void)run
{
weight -= 1; // 跑一次,体重减1
NSLog (@"学生跑完这次后的体重是%f", weight);
}
- (void)print
{
NSLog (@"性别= %d, 喜欢的颜色 = %d, 姓名 = %s, 生日 = %d-%d-%d",
sex, favcolor, name, birthday.year, birthday.month, birthday.day);
}
@end
int 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];
}
黑马程序员_类的合理设计_基本的属性&对象类型的属性
一、类的合理设计之基本的属性
类的合理设计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 { @public Sex sex; // 性别 Date birthday; // 生日 double weight; // 体重(kg) Color favColor; // 最喜欢的颜色 char *name; } - (void)eat; - (void)run; - (void)print; @end @implementation Student - (void)eat { weight += 1; // 每吃一次,体重就加1 NSLog (@"吃完这次后的体重是%f", weight); } - (void)run { weight -= 1; // 跑一次,体重减1 NSLog (@"跑完这次后的体重是%f", weight); } - (void)print { NSLog (@"性别= %d, 喜欢的颜色 = %d, 姓名 = %s, 生日 = %d-%d-%d", sex, favcolor, name, birthday.year, birthday.month, birthday.day); } @end int 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
{
@public
double weight; // 体重
Color curColor; // 毛色
}
- (void)eat;
- (void)run;
@end
@implementation Dog
- (void)eat
{
weight += 1; // 每吃一次,体重就加1
NSLog (@"狗吃完这次后的体重是%f", weight);
}
- (void)run
{
weight -= 1; // 跑一次,体重减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
@implementation Student
- (void)liuDog
{
// 让狗跑起来(调用狗的run方法)
[dog run];
}
- (void)weiDog
{
// 让狗吃东西(调用狗的eat方法)
[dog eat];
}
- (void)eat
{
weight += 1; // 每吃一次,体重就加1
NSLog (@"学生吃完这次后的体重是%f", weight);
}
- (void)run
{
weight -= 1; // 跑一次,体重减1
NSLog (@"学生跑完这次后的体重是%f", weight);
}
- (void)print
{
NSLog (@"性别= %d, 喜欢的颜色 = %d, 姓名 = %s, 生日 = %d-%d-%d",
sex, favcolor, name, birthday.year, birthday.month, birthday.day);
}
@end
int 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];
}
相关文章推荐
- C++中提供了多种基本的数据类型。实际上,这些远不能满足我们的需求,如复数(第10章的例子大多是处理虚数的),再如分数。本任务将设计一个简单的分数类,完成对分数的几个运算。一则巩固基于对象编程的方法,
- 黑马程序员__String_StringBuffer_基本数据类型对象包装类
- 黑马程序员------基本数据类型对象
- 黑马程序员_基本数据类型对象包装类
- 黑马程序员:操作基本数据类型的流对象:DataInputStream、DataOutputStream
- 黑马程序员_String和基本类型对象包装类总结
- 黑马程序员--基本数据类型对象包装类、String类、集合学习
- 黑马程序员 --- String类 StringBuffer 基本数据类型对象包装类
- 黑马程序员--高新技术--静态导入,基本数据类型拆箱与装箱,享元设计模式
- 黑马程序员_基本数据类型对象包装类新特性
- 39.黑马程序员-基本数据类型对象包装类
- 黑马程序员------IO流 操作字节数组的流对象,对象序列化,打印流,随机存储文件,合并流,管道流,操作基本数据类型的流对象
- 黑马程序员-JAVA基础-基本数据类型对象包装类
- iOS Json和对象互转(反射支持对象嵌套的复杂对象类型,支持基本数据类型和NSDate属性转化)
- 黑马程序员:API及基本数据类型的对象包装类
- 黑马程序员_学习记录12:String、StringBuffer、基本数据类型对象包装类
- 黑马程序员---StringBuffer和基本数据类型对象包装类
- 黑马程序员---字符串和基本数据类型对象包装类
- 黑马程序员_java-基本数据类型的对象包装类-Math类-Random类-Scanner类<六>
- 黑马程序员:基本数据类型对象包装类