用C语言写面向的对象是一种什么样的体验
作者:愤怒的呆鱼 发布时间:[ 2017/3/7 11:25:38 ] 推荐标签:测试开发技术 C语言
好了,在你们面前班门弄斧了一番,下面开始具体的代码实现:
头文件:
#ifndef CAR_H_
#define CAR_H_
#include <stdio.h>
#include <stdlib.h>
typedef enum CarType
{
CAR_BENZE = 0,
CAR_BMW,
CAR_AUDI,
CAR_NONE,
}CarType;
typedef struct Base_Car
{
CarType car_type;
void (* speaker)(struct Base_Car* car);
void* parent_car; //point to parent,if no any parent,then make it NULL
}Base_Car;
typedef struct Benze_Car
{
Base_Car* car;
void (* speaker)(struct Base_Car* car);
}Benze_Car;
typedef struct BMW_Car
{
Base_Car* car;
void (* speaker)(struct Base_Car* car);
}BMW_Car;
typedef struct Audi_Car
{
Base_Car* car;
void (* speaker)(struct Base_Car* car);
}Audi_Car;
typedef struct Car_Factory
{
Base_Car* (* create_new_car)(CarType car_type);
}Car_Factory;
Car_Factory* new_car_factory();
void delete_car_factory(Car_Factory* car_factory);
Base_Car* new_Base_Car();
Benze_Car* new_benze_Car();
BMW_Car* new_bmw_Car();
Audi_Car* new_audi_Car();
void delete_Base_Car(struct Base_Car* car);
void delete_Benze_Car(struct Benze_Car* car);
void delete_BMW_Car(struct BMW_Car* car);
void delete_Audi_Car(struct Audi_Car* car);
#endif /* CAR_H_ */
源文件:
#include "Car.h"
static void Car_speaker(struct Base_Car* car)
{
printf("this is a car
");
}
static void Benze_speaker(struct Base_Car* car)
{
printf("this is Benze Car, car type is :%d
",car->car_type);
}
static void BMW_speaker(struct Base_Car* car)
{
printf("this is BMW Car, car type is :%d
",car->car_type);
}
static void Audi_speaker(struct Base_Car* car)
{
printf("this is Audi Car, car type is :%d
",car->car_type);
}
Benze_Car* new_benze_Car()
{
Benze_Car* real_car = (Benze_Car*)malloc(sizeof(Benze_Car));
Base_Car* base_car = new_Base_Car();
printf("Benze_Car create
");
real_car->car = base_car;
real_car->speaker = Benze_speaker;
base_car->car_type = CAR_BENZE;
base_car->parent_car = (void*)real_car;
base_car->speaker = real_car->speaker;
return real_car;
}
BMW_Car* new_bmw_Car()
{
BMW_Car* real_car = (BMW_Car*)malloc(sizeof(BMW_Car));
Base_Car* base_car = new_Base_Car();
printf("BMW_Car create
");
base_car->car_type = CAR_BMW;
real_car->car = base_car;
real_car->speaker = BMW_speaker;
base_car->car_type = CAR_BMW;
base_car->parent_car = (void*)real_car;
base_car->speaker = real_car->speaker;
return real_car;
}
Audi_Car* new_audi_Car()
{
Audi_Car* real_car = (Audi_Car*)malloc(sizeof(Audi_Car));
Base_Car* base_car = new_Base_Car();
printf("Audi_Car create
");
base_car->car_type = CAR_AUDI;
real_car->car = base_car;
real_car->speaker = Audi_speaker;
base_car->car_type = CAR_AUDI;
base_car->parent_car = (void*)real_car;
base_car->speaker = real_car->speaker;
return real_car;
}
Base_Car* new_Base_Car()
{
Base_Car* base_car = (Base_Car*)malloc(sizeof(Base_Car));
printf("BaseCar create
");
base_car->car_type = CAR_NONE;
base_car->parent_car = NULL;
base_car->speaker = Car_speaker;
return base_car;
}
Base_Car* create_new_Car(CarType car_type)
{
Base_Car* base_car = NULL;
switch(car_type)
{
case CAR_BENZE:
{
Benze_Car* real_car = new_benze_Car();
base_car = real_car->car;
break;
}
case CAR_BMW:
{
BMW_Car* real_car = new_bmw_Car();
base_car = real_car->car;
break;
}
case CAR_AUDI:
{
Audi_Car* real_car = new_audi_Car();
base_car = real_car->car;
break;
}
default:
break;
}
return base_car;
}
void delete_Benze_Car(struct Benze_Car* car)
{
free(car->car);
car->car = NULL;
free(car);
printf("Benze_Car delete
");
}
void delete_BMW_Car(struct BMW_Car* car)
{
free(car->car);
car->car = NULL;
free(car);
printf("BMW_Car delete
");
}
void delete_Audi_Car(struct Audi_Car* car)
{
free(car->car);
car->car = NULL;
free(car);
printf("Audi_Car delete
");
}
void delete_Base_Car(struct Base_Car* car)
{
if(NULL != car->parent_car)
{
switch(car->car_type)
{
case CAR_BENZE:
{
delete_Benze_Car((Benze_Car*)car->parent_car);
car = NULL; //base car will be delete in child free function
break;
}
case CAR_BMW:
{
delete_BMW_Car((BMW_Car*)car->parent_car);
car = NULL;
break;
}
case CAR_AUDI:
{
delete_Audi_Car((Audi_Car*)car->parent_car);
car = NULL;
break;
}
default:
break;
}
}
if(NULL != car)
{
free(car);
car = NULL;
}
printf("Base_Car delete
");
}
Car_Factory* new_car_factory()
{
Car_Factory* car_factory = (Car_Factory*)malloc(sizeof(Car_Factory));
car_factory->create_new_car = create_new_Car;
return car_factory;
}
void delete_car_factory(Car_Factory* car_factory)
{
free(car_factory);
car_factory = NULL;
}
测试文件main.cpp
#include <stdio.h>
#include "Car.h"
int main()
{
Car_Factory* car_factory = new_car_factory();
Base_Car* benzeCar = car_factory->create_new_car(CAR_BENZE);
Base_Car* bmwCar = car_factory->create_new_car(CAR_BMW);
Base_Car* audiCar = car_factory->create_new_car(CAR_AUDI);
benzeCar->speaker(benzeCar);
bmwCar->speaker(bmwCar);
audiCar->speaker(audiCar);
delete_Base_Car(benzeCar);
benzeCar = NULL;
delete_Base_Car(bmwCar);
bmwCar = NULL;
delete_Base_Car(audiCar);
audiCar = NULL;
delete_car_factory(car_factory);
car_factory = NULL;
return 0;
}
编译后执行:
以上的结果可以看出,我们的测试代码接口都是一样的,效果达到了C++面向对象的设计理念,用C语言完成了一次狠狠的逆袭,希望读者朋友在你的项目工程中有帮助。其实程序员的工作大部分是写代码,但是代码的阅读对象往往并不是我们自己,将我们的思维写进去才是一个程序员的境界,不要简单的根据流程去写一个代码,否则,程序员真的只是一个工具了;
哦,BTW,在函数中我使用了本结构体的指针在里面,是为了达到在函数中使用示例的属性,这样独立每一个示例的属性操作了。
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11