LW_OOPC.H 面向对象C MISOO 头文件

Use Case(用例)的主要内涵及其表达为:

1)描述该服务的名称及返回的信息类型,她描述What

2)描述服务过程中,用户与系统之间有哪些交互,其描述How和When。但是请留意,这个How不是员工作汉堡的How,而是用户与系统如何交互的How。

3)描述用户的角色,她描述Who

/////////////////////////////////////////////////////////////////////////////////////

动态型(Dynamic):使用malloc函数

静态型(Static):标准型;不用malloc,其对象不是在程序执行期间才去分配空间的。而是与一般的int,char形态的变量一样,在编译期间就留下内存空间了,不需要在执行期间通过malloc来取得内存空间。

纯粹静态型(pure STATIC)精简型;避免使用指针来调用函数。即不调用new函数。

/*  lw_oopc_kc.h */ 
#ifndef LOOPC_H 
#define LOOPC_H 
#include <malloc.h> 
#define CLASS(type)/ 
typedef struct type type; / 
struct type
#ifndef LW_OOPC_PURE_STATIC 
#ifndef LW_OOPC_STATIC 
#ifndef LW_OOPC_DYNAMIC
#define CTOR(type) / 
void* type##Setting(type*); / 
void* type##New() / 
       { / 
         struct type *t; / 
         t = (struct type *)malloc(sizeof(struct type)); / 
         return type##Setting(t); / 
       } / 
void* type##Setting(type *t) / 
      { 
    #else 
      #define CTOR(type) / 
      void* type##New() / 
      { / 
       struct type* t; / 
       t = (struct type*)malloc(sizeof(struct type)); 
    #endif
    #else 
    #define CTOR(type) / 
    void * type##Setting(type* t)/ 
    { 
        #endif 
        #endif
#define END_CTOR return (void*)t;  } 
#define FUNCTION_SETTING(f1, f2)  t->f1 = f2; 
#define IMPLEMENTS(type) type type 
#define INTERFACE(type) / 
typedef struct type type; / 
struct type 
#endif 
/*     end     */


lw_oopc.h是一个基于C语言实现的简单的面向对象编程库,以下是该文件的注释: ``` /* * lw_oopc.h * * A simple implementation of Object-Oriented Programming in C * by Liang Weihan (liangweihan0426@gmail.com) * * This library provides a way to define classes and create objects in C, * using a structure to represent the object and function pointers to * represent methods. * * Usage: * #include "lw_oopc.h" * * // Define a class * class_def(MyClass) { * // private variables * int x; * * // public methods * int (*get_x)(MyClass *self); * void (*set_x)(MyClass *self, int x); * }; * * // Implement the methods * impl(MyClass, get_x) { * return self->x; * } * * impl(MyClass, set_x) { * self->x = x; * } * * // Create an object * MyClass *obj = new(MyClass); * * // Call the methods * obj->set_x(obj, 42); * printf("%d\n", obj->get_x(obj)); * * // Destroy the object * delete(obj); * */ #ifndef LW_OOPC_H_ #define LW_OOPC_H_ // Class definition macro #define class_def(classname) \ typedef struct classname classname; \ struct classname // Method implementation macro #define impl(classname, methodname) \ int (*methodname)(classname *self) // Constructor macro #define new(classname) \ ({ \ classname *self = (classname *) malloc(sizeof(classname)); \ memset(self, 0, sizeof(classname)); \ self; \ }) // Destructor macro #define delete(obj) \ ({ \ free(obj); \ }) #endif /* LW_OOPC_H_ */ ``` 该文件实现了一个简单的面向对象编程框架,使用结构体来表示对象,使用函数指针来表示方法。通过class_def宏来定义类,impl宏来实现方法,new宏来创建对象,delete宏来销毁对象。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值