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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | #ifndef __MAN_H__ #define __MAN_H__ #include "boy.h" #define MAN_TYPE (man_get_type()) #define MAN(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),MAN_TYPE,Man)) typedef struct _Man Man; typedef struct _ManClass ManClass; struct _Man { Boy parent; char *job; void (*bye)(void); }; struct _ManClass { BoyClass parent_class; }; GType man_get_type(void); Man* man_new(void); gchar* man_get_gob(Man *man); void man_set_job(Man *man, gchar *job); Man* man_new_with_name_age_and_job(gchar *name, gint age, gchar *job); void man_info(Man *man); #endif //__MAN_H__ /* man.c */ #include "man.h" static void man_bye(void); static void man_init(Man *man); static void man_class_init(Man *man); GType man_get_type(void) { static GType man_type = 0; if(!man_type) { static const GTypeInfo man_info = { sizeof(ManClass), NULL, NULL, (GClassInitFunc)man_class_init, NULL, NULL, sizeof(Man), 0, (GInstanceInitFunc)man_init }; man_type = g_type_register_static(BOY_TYPE, "Man", &man_info, 0); } return man_type; } static void man_init(Man *man) { man->job = "none"; man->bye = man_bye; } static void man_class_init(Man *man) { } Man* man_new(void) { Man *man; man = g_object_new(MAN_TYPE, 0); return man; } gchar* man_get_gob(Man *man) { return man->job; } void man_set_job(Man *man, gchar *job) { man->job = job; } Man* man_new_with_name_age_and_job(gchar *name, gint age, gchar *job) { Man *man; man = man_new(); boy_set_name(BOY(man), name); boy_set_age(BOY(man), age); man_set_job(man, job); return man; } static void man_bye(void) { g_print("Goodbye everyone !\n"); } void man_info(Man *man) { g_print("the man name is %s\n", BOY(man)->name); g_print("the man age is %d\n", BOY(man)->age); g_print("the man job is %s\n", man->job); } |
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | #include <glib.h> #include "boy.h" #include "man.h" int main(int argc, char *argv[]) { Boy *tom, *peter; Man *green, *brown; g_type_init();//注意,初始化类型系统,必需 tom = boy_new_with_name("Tom"); tom->cry(); boy_info(tom); peter = boy_new_with_name_and_age("Peter", 10); peter->cry(); boy_info(peter); green = man_new(); boy_set_name(BOY(green), "Green"); //设定Man对象的name属性用到其父对象Boy的方法 boy_set_age(BOY(green), 28); man_set_job(green, "Doctor"); green->bye(); man_info(green); brown = man_new_with_name_age_and_job("Brown", 30, "Teacher"); brown->bye(); man_info(brown); } Makefile文件如下: CC = gcc all: $(CC) -c boy.c `pkg-config --cflags glib-2.0 gobject-2.0` $(CC) -c man.c `pkg-config --cflags glib-2.0 gobject-2.0` $(CC) -c main.c `pkg-config --cflags glib-2.0 gobject-2.0` $(CC) -o simple boy.o man.o main.o `pkg-config --libs glib-2.0 gobject-2.0` 执行make命令编译,编译结束后,执行./simple运行此测试程序,输出结果如下: Message : A boy was born . The Boy is crying ...... The Boy name is Tom The Boy age is 0 Message : A boy was born . The Boy is crying ...... The Boy name is Peter The Boy age is 10 Goodbye everyone ! the man name is Green the man age is 28 the man job is Doctor Goodbye everyone ! the man name is Brown the man age is 30 the man job is Teacher |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |