-
Notifications
You must be signed in to change notification settings - Fork 0
/
Orange.h
35 lines (33 loc) · 1.07 KB
/
Orange.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "AbstractGoods.h"
struct Orange {
struct AbstractGoodsOps abGoods;
int num;
};
void orange_increaseNum(struct AbstractGoodsOps *abGoods) {
struct Orange *orange = container_of(abGoods, struct Orange, abGoods);
orange->num++;
}
void orange_decreaseNum(struct AbstractGoodsOps *abGoods) {
struct Orange *orange = container_of(abGoods, struct Orange, abGoods);
orange->num--;
}
int orange_getPrice(struct AbstractGoodsOps *abGoods) {
return 10;
}
int orange_getNum(struct AbstractGoodsOps *abGoods) {
struct Orange *orange = container_of(abGoods, struct Orange, abGoods);
return orange->num;
}
struct AbstractGoodsOps orangeOps = {
.getPrice = (int (*)(void *))orange_getPrice,
.getNum = (int (*)(void *))orange_getNum,
.increaseNum = (void (*)(void *))orange_increaseNum,
.decreaseNum = (void (*)(void *))orange_decreaseNum,
};
struct Orange *Orange() {
struct Orange *orange = (struct Orange *)malloc(sizeof(*orange));
orange->abGoods = (orangeOps);
orange->abGoods.list.next = NULL;
orange->num = 0;
return orange;
}