黑马程序员_类的合理设计_基本的属性&对象类型的属性



黑马程序员_类的合理设计_基本的属性&对象类型的属性

一、类的合理设计之基本的属性

类的合理设计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];
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值