使用gtype构造的一个简单学生管理程序其他属性自己加

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);
}

     
     
    
    
   
   
  
  
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值