person.h: #ifndef _PERSON_H #define _PERSON_H #include #include #include #define PERSON_TYPE ( person_get_type() ) #define PERSON(obj) ( G_TYPE_CHECK_INSTANCE_CAST((obj), PERSON_TYPE, CPerson) ) #define PERSON_CLASS(klass) ( G_TYPE_CHECK_CLASS_CAST((klass), PERSON_TYPE, CPersonClass ) ) #define IS_PERSON(obj) ( G_TYPE_CHECK_INSTANCE_TYPE((obj), PERSON_TYPE) ) #define IS_PERSONCALSS(klass) ( G_TYPE_CHECK_CLASS_TYPE((klass), PERSON_TYPE) ) #define PERSON_GET_CLASS(obj) ( G_TYPE_INSTANCE_GET_CLASS(), PERSON_TYPE, CPersonClass ) typedef struct _CPerson CPerson; typedef struct _CPersonClass CPersonClass; struct _CPerson { GObject parent; gchar* name; }; struct _CPersonClass { GObjectClass parent_class; }; /* all of internal interface */ GType person_get_type(void); /*{@ property definition for person */ #define PERSON_PROPERTY_INDEX_NAME 1 /*@} end definition */ /* print function name*/ #define PERSON_PRINT_SELF() g_printf("%s/n",__FUNCTION__); /* user interface */ CPerson* person_new(); #endif/* person.h*/ person.c: #include "person.h" #include #include static void person_set_property( GObject* object, guint property_id, GValue *value, GParamSpec* spec); static void person_get_property( GObject* object, guint property_id, GValue *value, GParamSpec* spec); static void person_init( CPerson* person) { g_print("%s/n", __FUNCTION__); person->name = "none"; } static void class_init(CPersonClass* klass) { GObjectClass* obj_class = G_OBJECT_CLASS(klass); obj_class->set_property = person_set_property; obj_class->get_property = person_get_property; g_object_class_install_property(obj_class,1, g_param_spec_string("name", "name", "name of this person", "none", G_PARAM_READABLE|G_PARAM_WRITABLE)); PERSON_PRINT_SELF(); } GType person_get_type(void) { static GType person_type = 0; if(!person_type){ GTypeInfo info = { sizeof(CPersonClass), /* base init&finalize*/ NULL, NULL, /*Class init & finalize*/ (GClassInitFunc)class_init, NULL, NULL, sizeof(CPerson), 0, (GInstanceInitFunc)person_init }; person_type = g_type_register_static( G_TYPE_OBJECT, "person", &info, 0); } return person_type; } static void person_set_property( GObject* object, guint property_id, GValue *value, GParamSpec* spec) { CPerson* person = PERSON(object); PERSON_PRINT_SELF(); switch(property_id) { case 1: person->name = g_value_dup_string(value); break; default: break; } } static void person_get_property( GObject* object, guint property_id, GValue *value, GParamSpec* spec) { CPerson* person = PERSON(object); PERSON_PRINT_SELF(); switch(property_id) { case 1: g_value_set_string(value, person->name); break; default: break; } } CPerson* person_new() { CPerson* person = 0; //if used the gtype system, we'd call this API for init. g_type_init(); person = g_object_new(PERSON_TYPE, NULL); return person; } main.c: #include "person.h" gint main(gint argc, gchar* argv[]) { CPerson* you=0; gchar* name = 0; you = person_new(); g_object_set(G_OBJECT(you),"name", "test", NULL); g_object_get(G_OBJECT(you), "name", &name, NULL); g_print("default name=%s/n", name); }
使用gtype构造的一个简单学生管理程序其他属性自己加
最新推荐文章于 2024-02-27 10:12:37 发布