C语言封装继承多态
时间:2010-09-11 来源:wsnhyjj
#include <stdio.h>
#include <conio.h>
#define MAX 10
typedef struct Animal{ void *this; char name[MAX]; void (*shout)(struct Animal *); }Animal;
typedef struct Swimmable{ void *this; void (*swim)(struct Swimmable *); }Swimmable;
typedef struct Dog{ Animal *Animal; Swimmable *Swimmable; }Dog;
typedef struct Cat{ Animal *Animal; Swimmable *Swimmable; }Cat;
void shout_Cat(Animal *cat){ printf("%s miaomiao.\n", cat->name); }
void swim_Cat(Swimmable *cat){ printf("%s cannot swim.\n", ((Cat *)(cat->this))->Animal->name); }
void shout_Dog(Animal *dog){ printf("%s wangwang.\n", dog->name); }
void swim_Dog(Swimmable *dog){ printf("%s can swim.\n", ((Dog *)(dog->this))->Animal->name); }
void visit_Animal(Animal *animal){ animal->shout(animal); }
void visit_Swimmable(Swimmable *swimmable){ swimmable->swim(swimmable); }
void initialize_Dog(Dog* dog, char *name){ dog->Animal = (Animal *) malloc(sizeof(Animal)); dog->Animal->this = dog; dog->Animal->shout = shout_Dog; strcpy(dog->Animal->name, name); dog->Swimmable = (Swimmable *) malloc(sizeof(Swimmable)); dog->Swimmable->this = dog; dog->Swimmable->swim = swim_Dog; }
void initialize_Cat(Cat* cat, char *name){ cat->Animal = (Animal *) malloc(sizeof(Animal)); cat->Animal->this = cat; cat->Animal->shout = shout_Cat; strcpy(cat->Animal->name, name); cat->Swimmable = (Swimmable *) malloc(sizeof(Swimmable)); cat->Swimmable->this = cat; cat->Swimmable->swim = swim_Cat; }
void finalize_Dog(Dog *dog){ free(dog->Animal); free(dog->Swimmable); }
void finalize_Cat(Cat *cat){ free(cat->Animal); free(cat->Swimmable); }
void main(){ Dog dog; Cat cat; clrscr(); initialize_Dog(&dog, "Wangcai"); initialize_Cat(&cat, "Kitty"); visit_Animal(dog.Animal); visit_Animal(cat.Animal); visit_Swimmable(dog.Swimmable); visit_Swimmable(cat.Swimmable); finalize_Dog(&dog); finalize_Cat(&cat); }
typedef struct Animal{ void *this; char name[MAX]; void (*shout)(struct Animal *); }Animal;
typedef struct Swimmable{ void *this; void (*swim)(struct Swimmable *); }Swimmable;
typedef struct Dog{ Animal *Animal; Swimmable *Swimmable; }Dog;
typedef struct Cat{ Animal *Animal; Swimmable *Swimmable; }Cat;
void shout_Cat(Animal *cat){ printf("%s miaomiao.\n", cat->name); }
void swim_Cat(Swimmable *cat){ printf("%s cannot swim.\n", ((Cat *)(cat->this))->Animal->name); }
void shout_Dog(Animal *dog){ printf("%s wangwang.\n", dog->name); }
void swim_Dog(Swimmable *dog){ printf("%s can swim.\n", ((Dog *)(dog->this))->Animal->name); }
void visit_Animal(Animal *animal){ animal->shout(animal); }
void visit_Swimmable(Swimmable *swimmable){ swimmable->swim(swimmable); }
void initialize_Dog(Dog* dog, char *name){ dog->Animal = (Animal *) malloc(sizeof(Animal)); dog->Animal->this = dog; dog->Animal->shout = shout_Dog; strcpy(dog->Animal->name, name); dog->Swimmable = (Swimmable *) malloc(sizeof(Swimmable)); dog->Swimmable->this = dog; dog->Swimmable->swim = swim_Dog; }
void initialize_Cat(Cat* cat, char *name){ cat->Animal = (Animal *) malloc(sizeof(Animal)); cat->Animal->this = cat; cat->Animal->shout = shout_Cat; strcpy(cat->Animal->name, name); cat->Swimmable = (Swimmable *) malloc(sizeof(Swimmable)); cat->Swimmable->this = cat; cat->Swimmable->swim = swim_Cat; }
void finalize_Dog(Dog *dog){ free(dog->Animal); free(dog->Swimmable); }
void finalize_Cat(Cat *cat){ free(cat->Animal); free(cat->Swimmable); }
void main(){ Dog dog; Cat cat; clrscr(); initialize_Dog(&dog, "Wangcai"); initialize_Cat(&cat, "Kitty"); visit_Animal(dog.Animal); visit_Animal(cat.Animal); visit_Swimmable(dog.Swimmable); visit_Swimmable(cat.Swimmable); finalize_Dog(&dog); finalize_Cat(&cat); }
相关阅读 更多 +