软件设计模式

软件设计模式

软件设计模式(Design pattern),又称设计模式,是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。使用设计模式是为了可重用代码、让代码更容易被他人理解、保证代码可靠性、程序的重用性。总的来说:就是代码设计经验的总结,能够让代码稳定,拓展性更强,一系列编程的思想。设计模式有23种,代码容易被他人理解,保证代码可靠性,程序的重用性,要慢慢的积累。

算法:

算法不是设计模式,因为算法致力于解决问题而非设计问题。设计模式通常描述了一组相互紧密作用的类与对象。
百科讲解:https://baike.baidu.com/item/%E8%BD%AF%E4%BB%B6%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/2117635?fr=aladdin

什么是类和对象
类是一种用户定义的引用数据类型,也称类类型。(在C语言中和结构体差不多)
对象:类的一种具象。

struct Animal{
	char name[32];
	int age;
	int sex;
	void (*peat)();
	void (*pbeat)();  //dog cat 是类(Animal)的一种对象
};

struct Animal cat; 
struct Animal dog;	//dog  是类(Animal)

C语言面向对象示例

#include <stdio.h>

struct Animal{
	char name[32];
	int age;
	int sex;
	void (*peat)();
	void (*pbeat)();   //dog cat 是类(Animal)的一种对象

};
void dogEat()
{
	printf("dog eat\n");
	return ;
}
void dogBeat()
{
	printf("yao\n");
	return ;
}
void catEat()
{
	printf("cat eat\n");
	return ;
}
void catBeat()
{
	printf("zhua\n");
	return ;
}



int main(void)
{	
	struct Animal cat; 
	struct Animal dog;	//dog  是类(Animal)

	cat.peat = catEat;
	cat.pbeat = catBeat;
	dog.peat = dogEat;
	dog.pbeat = catBeat;

	cat.peat();
	cat.pbeat();
	dog.peat();
	dog.pbeat();
	

	return 0;
}

C结构体另一种用法

#include <stdio.h>

struct Animal{
	char name[32];
	int age;
	int sex;
	void (*peat)();
	void (*pbeat)();   //dog cat 是类(Animal)的一种对象

};
void dogEat()
{
	printf("dog eat\n");
	return ;
}
void dogBeat()
{
	printf("yao\n");
	return ;
}
void catEat()
{
	printf("cat eat\n");
	return ;
}
void catBeat()
{
	printf("zhua\n");
	return ;
}



int main(void)
{	//结构体新的用法
	struct Animal cat = {
		.peat = catEat,
		.pbeat = catBeat
	};
	struct Animal dog = {				//dog  是类(Animal)
		.peat = dogEat,
		.pbeat = dogBeat
	};            			

	cat.peat();
	cat.pbeat();

	dog.peat();
	dog.pbeat();

	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值