使用gtype構造的一個簡單學生管理程式其他屬性自己加

來源:互聯網
上載者:User
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);}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.