面线对象引入对象和类的概念(C语言)

软件的设计模式:

————23种
————代码设计的经验和总结,
————主要的作用:使代码运行稳定、拓展、可读性更强

目前学习的23种里面的方法是之一:
					————工厂模式

描述了一组相互紧密作用的类和对象
//类这是一种用户定义的引用他=数据类型,也称类型
//结构体就是一种类
//对象:类的一种具象(例如struct animal dog ,dog就是一个对象)

code:OPP1.c

#include <stdio.h>

struct animal{
	int age;
	int sex;
	int other;	//成员属性
	void (*peat)();
	void (*pcoitus)();//成员方法

};

void dogEat()
{
	printf("狗喜欢吃屎\n");
}

void catEat()
{
	printf("猫喜欢吃鱼\n");
}

void humanEat()
{
	printf("人什么都吃\n");
}


void dogCoitus()
{
	printf("dog do love is promiscuity\n");
}

void catCoitus()
{
	printf("cat do love is promiscuity\n");
}

void humanCoitus()
{
	printf("preson do love is normal\n");
}

int main(void)
{
	struct animal	dog;
	struct animal	cat;
	struct animal	human; //对象,事务的具象
	
	dog.peat = dogEat;
	cat.peat = catEat;
	human.peat = humanEat;
	
	dog.pcoitus = dogCoitus;
	cat.pcoitus = catCoitus;
	human.pcoitus = humanCoitus;
	
	dog.peat();
	dog.pcoitus();
	cat.peat();
	cat.pcoitus();
	human.peat();
	human.pcoitus();

	return 0;
}
#include <stdio.h>
//类这是一种用户定义的引用他=数据类型,也称类型
//结构体就是一种类
//对象:类的一种具象

struct animal{
	int age;
	int sex;
	int other;	//成员属性
	void (*peat)();
	void (*pcoitus)();//成员方法

};

void dogEat()
{
	printf("狗喜欢吃屎\n");
}

void catEat()
{
	printf("猫喜欢吃鱼\n");
}

void humanEat()
{
	printf("人什么都吃\n");
}


void dogCoitus()
{
	printf("dog do love is promiscuity\n");
}

void catCoitus()
{
	printf("cat do love is promiscuity\n");
}

void humanCoitus()
{
	printf("preson do love is normal\n");
}

int main(void)
{
	//结构体常用的定义玩法
	struct animal dog={
		.peat = dogEat,
		.pcoitus = dogCoitus
	};
	struct animal cat={
		.peat = catEat,
		.pcoitus = catCoitus
	};
	struct animal human={
		.peat = humanEat,
		.pcoitus = humanCoitus
	};
	
	dog.peat();
	dog.pcoitus();
	cat.peat();
	cat.pcoitus();
	human.peat();
	human.pcoitus();
	
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值