软件的设计模式:
————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;
}