📄 726.html
字号:
#ifndef __MAN_H__<br>
#define __MAN_H__<br>
<br>
#include "boy.h"<br>
<br>
#define MAN_TYPE (man_get_type())<br>
#define MAN(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),MAN_TYPE,Man))<br>
<br>
typedef struct _Man Man;<br>
typedef struct _ManClass ManClass;<br>
<br>
struct _Man {<br>
Boy parent;<br>
<br>
char *job;<br>
void (*bye)(void);<br>
};<br>
<br>
struct _ManClass {<br>
BoyClass parent_class;<br>
};<br>
<br>
GType man_get_type(void);<br>
Man* man_new(void);<br>
gchar* man_get_gob(Man *man);<br>
void man_set_job(Man *man, gchar *job);<br>
Man* man_new_with_name_age_and_job(gchar *name, gint age, gchar *job);<br>
void man_info(Man *man);<br>
<br>
#endif //__MAN_H__<br>
<br>
<br>
/* man.c */<br>
#include "man.h"<br>
<br>
static void man_bye(void);<br>
<br>
static void man_init(Man *man);<br>
static void man_class_init(Man *man);<br>
<br>
GType man_get_type(void)<br>
{<br>
static GType man_type = 0;<br>
if(!man_type)<br>
{<br>
static const GTypeInfo man_info = {<br>
sizeof(ManClass),<br>
NULL, NULL,<br>
(GClassInitFunc)man_class_init,<br>
NULL, NULL,<br>
sizeof(Man),<br>
0,<br>
(GInstanceInitFunc)man_init<br>
};<br>
man_type = g_type_register_static(BOY_TYPE, "Man", &man_info, 0);<br>
}<br>
return man_type;<br>
<br>
}<br>
<br>
static void man_init(Man *man)<br>
{<br>
man->job = "none";<br>
man->bye = man_bye;<br>
}<br>
<br>
static void man_class_init(Man *man)<br>
{<br>
}<br>
<br>
Man* man_new(void)<br>
{<br>
Man *man;<br>
man = g_object_new(MAN_TYPE, 0);<br>
return man;<br>
}<br>
<br>
gchar* man_get_gob(Man *man)<br>
{<br>
return man->job;<br>
}<br>
<br>
void man_set_job(Man *man, gchar *job)<br>
{<br>
man->job = job;<br>
}<br>
<br>
Man* man_new_with_name_age_and_job(gchar *name, gint age, gchar *job)<br>
{<br>
Man *man;<br>
man = man_new();<br>
boy_set_name(BOY(man), name);<br>
boy_set_age(BOY(man), age);<br>
man_set_job(man, job);<br>
return man;<br>
}<br>
<br>
static void man_bye(void)<br>
{<br>
g_print("Goodbye everyone !");<br>
}<br>
<br>
void man_info(Man *man)<br>
{<br>
g_print("the man name is %s", BOY(man)->name);<br>
g_print("the man age is %d", BOY(man)->age);<br>
g_print("the man job is %s", man->job);<br>
}<br>
<br>
关键在于定义对象时将父对象实例定义为Boy,父类设定为BoyClass,在注册此对象时将其父对象类型设为BOY_TYPE,在设定对象属性时如用到父对象的属性要强制转换下,如取得对象的name属性,就必须用BOY(obj)->name,因为Man本身没有name属性,而其父对象Boy 有,所以用BOY宏将其强制为Boy类型的对象。<br>
<br>
测试我们定义的对象<br>
<br>
<br>
#include <glib.h><br>
<br>
#include "boy.h"<br>
#include "man.h"<br>
<br>
int main(int argc, char *argv[])<br>
{<br>
Boy *tom, *peter;<br>
Man *green, *brown; <br>
<br>
g_type_init();//注意,初始化类型系统,必需<br>
<br>
tom = boy_new_with_name("Tom");<br>
tom->cry();<br>
boy_info(tom);<br>
<br>
peter = boy_new_with_name_and_age("Peter", 10);<br>
peter->cry();<br>
boy_info(peter);<br>
<br>
green = man_new();<br>
boy_set_name(BOY(green), "Green");<br>
//设定Man对象的name属性用到其父对象Boy的方法<br>
boy_set_age(BOY(green), 28);<br>
man_set_job(green, "Doctor");<br>
green->bye();<br>
man_info(green);<br>
<br>
brown = man_new_with_name_age_and_job("Brown", 30, "Teacher");<br>
brown->bye();<br>
man_info(brown);<br>
<br>
}<br>
<br>
Makefile文件如下:<br>
<br>
CC = gcc<br>
all:<br>
$(CC) -c boy.c `pkg-config --cflags glib-2.0 gobject-2.0`<br>
$(CC) -c man.c `pkg-config --cflags glib-2.0 gobject-2.0`<br>
$(CC) -c main.c `pkg-config --cflags glib-2.0 gobject-2.0`<br>
$(CC) -o simple boy.o man.o main.o `pkg-config --libs glib-2.0 gobject-2.0`<br>
<br>
执行make命令编译,编译结束后,执行./simple运行此测试程序,输出结果如下:<br>
<br>
Message : A boy was born .<br>
The Boy is crying ......<br>
The Boy name is Tom<br>
The Boy age is 0<br>
Message : A boy was born .<br>
The Boy is crying ......<br>
The Boy name is Peter<br>
The Boy age is 10<br>
Goodbye everyone !<br>
the man name is Green<br>
the man age is 28<br>
the man job is Doctor<br>
Goodbye everyone !<br>
the man name is Brown<br>
the man age is 30<br>
the man job is Teacher<br>
<br>
Makefile中用到`pkg-config -cflags -libs gobject-2.0`,在GLIB中将线程(gthread),插件(gmoudle)和对象系统(gobject)这三个子系统区别对待,编译时要注意加入相应的参数。<br>
<br>
本文只是概要的介绍了如何定义和实现GObject对象,GObject系统中还有很多相关内容,如:枚举和标识类型(Enumeration and flags types);Gboxed,是Gtype系统中注册一种封装为不透明的C语言结构类型的机制;许多对象用到的参数对象都是C结构类型,使用者不必了解其结构的内部定义,即不透明,GBoxed即是实现这一功能的机制;标准的参数和变量类型的定义(Standard Parameter and Value Types)等,它们都以C语言来开发,是深入了解和掌握GObject的关键。<br>
<br>
透过以上代码实现,我们还可以看出,以GLIB为基础的GTK+/GNOME开发环境所具有的独特的编程风格和独到的开发思想。这一点在长期的编程实践中会体验得更深刻。<br>
<br>
有了GObject系统这一基础,GTK+通过它将X窗口环境中的控件(Widget)巧妙的封装起来,这使开发LINUX平台上的GUI应用程序更方便,更快捷。<br>
<br>
<br>
<br>
以上代码在Redhat 8.0 Linux平台,GLIB2.2.1环境下编译通过。<br>
<br>
感谢<br>
本文的写作参考了中国LINUX论坛的网友hoyt的文章,可以在gtkvb.cffd.org.cn找到,在此表示感谢。<br>
<br>
参考资料<br>
<br>
* 本文的所有代码可以在这里下载。<br>
* Thomas Hunger写的文章:Gobject tutorial<br>
* 中国 linux 论坛的网友 hoyt 的网页 http://gtkvb.cosoft.org.cn/ 上的文章<br>
* 与本文相关的另一篇文章《浅析GLib》<br>
<br>
<br>
关于作者<br>
宋国伟,乡村小学英语教师,他是《GTK+2.0编程范例》(清华大学出版社出版)一书的作者,业余时间致力于用GTK+开发LINUX GUI应用程序,可以通过电子邮件地址 gwsong52@sohu.com 与他联系。<br>
<br>
返回前一页<br>
<br>
本站由张善友制作并维护<br>
<br>
copyright 2002-2003 All rights reserved
</FONT><br>
</TD>
</TR>
<TR>
<TD colSpan=2><FONT
class=middlefont></FONT><BR>
<FONT
class=normalfont>全文结束</FONT> </TD>
</TR>
<TR>
<TD background="images/dot.gif" tppabs="http://www.linuxhero.com/docs/images/dot.gif" colSpan=2
height=10></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></DIV></TD>
<TD vAlign=top width="20%"
background="images/line.gif" tppabs="http://www.linuxhero.com/docs/images/line.gif" rowSpan=2>
<DIV align=center>
<table class=tableoutline cellspacing=1 cellpadding=4
width="100%" align=center border=0>
<tr class=firstalt>
<td noWrap background="images/bgline.gif" tppabs="http://www.linuxhero.com/docs/images/bgline.gif" colspan=2 height=21>
<font class=normalfont><b>所有分类</b></font></td>
</tr>
<tr class=secondalt> <td noWrap width=27%> <font class=normalfont>1:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type1.html" tppabs="http://www.linuxhero.com/docs/type1.html">非技术类</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>2:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type2.html" tppabs="http://www.linuxhero.com/docs/type2.html">基础知识</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>3:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type3.html" tppabs="http://www.linuxhero.com/docs/type3.html">指令大全</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>4:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type4.html" tppabs="http://www.linuxhero.com/docs/type4.html">shell</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>5:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type5.html" tppabs="http://www.linuxhero.com/docs/type5.html">安装启动</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>6:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type6.html" tppabs="http://www.linuxhero.com/docs/type6.html">xwindow</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>7:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type7.html" tppabs="http://www.linuxhero.com/docs/type7.html">kde</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>8:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type8.html" tppabs="http://www.linuxhero.com/docs/type8.html">gnome</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>9:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type9.html" tppabs="http://www.linuxhero.com/docs/type9.html">输入法类</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>10:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type10.html" tppabs="http://www.linuxhero.com/docs/type10.html">美化汉化</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>11:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type11.html" tppabs="http://www.linuxhero.com/docs/type11.html">网络配置</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>12:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type12.html" tppabs="http://www.linuxhero.com/docs/type12.html">存储备份</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>13:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type13.html" tppabs="http://www.linuxhero.com/docs/type13.html">杂项工具</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>14:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type14.html" tppabs="http://www.linuxhero.com/docs/type14.html">编程技术</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>15:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type15.html" tppabs="http://www.linuxhero.com/docs/type15.html">网络安全</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>16:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type16.html" tppabs="http://www.linuxhero.com/docs/type16.html">内核技术</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>17:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type17.html" tppabs="http://www.linuxhero.com/docs/type17.html">速度优化</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>18:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type18.html" tppabs="http://www.linuxhero.com/docs/type18.html">apache</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>19:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type19.html" tppabs="http://www.linuxhero.com/docs/type19.html">email</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>20:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type20.html" tppabs="http://www.linuxhero.com/docs/type20.html">ftp服务</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>21:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type21.html" tppabs="http://www.linuxhero.com/docs/type21.html">cvs服务</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>22:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type22.html" tppabs="http://www.linuxhero.com/docs/type22.html">代理服务</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>23:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type23.html" tppabs="http://www.linuxhero.com/docs/type23.html">samba</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>24:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type24.html" tppabs="http://www.linuxhero.com/docs/type24.html">域名服务</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>25:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type25.html" tppabs="http://www.linuxhero.com/docs/type25.html">网络过滤</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>26:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type26.html" tppabs="http://www.linuxhero.com/docs/type26.html">其他服务</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>27:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type27.html" tppabs="http://www.linuxhero.com/docs/type27.html">nfs</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>28:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type28.html" tppabs="http://www.linuxhero.com/docs/type28.html">oracle</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>29:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type29.html" tppabs="http://www.linuxhero.com/docs/type29.html">dhcp</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>30:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type30.html" tppabs="http://www.linuxhero.com/docs/type30.html">mysql</a></font></td> </tr> </table></td></tr><tr class=secondalt> <td noWrap width=27%> <font class=normalfont>31:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type31.html" tppabs="http://www.linuxhero.com/docs/type31.html">php</a></font></td> </tr> </table></td></tr><tr class=firstalt> <td noWrap width=27%> <font class=normalfont>32:</font> </td><td noWrap width=73%> <table width=100% border=0> <tr> <td><font class=normalfont><a href="type32.html" tppabs="http://www.linuxhero.com/docs/type32.html">ldap</a></font></td> </tr> </table></td></tr> </table>
</DIV></TD></TR>
<TR vAlign=top>
<TD width="80%">
<DIV align=center><BR>
</DIV>
</TD></TR></TBODY></TABLE></TD></TR>
</TABLE></TD></TR>
</TABLE>
<TABLE cellSpacing=0 cellPadding=4 width="100%" bgColor=#eeeeee
border=0><TBODY>
<TR>
<TD width="50%">
<P><FONT class=middlefont>版权所有 © 2004 <A
href="mailto:bjchenxu@sina.com">linux知识宝库</A><BR>
违者必究. </FONT></P>
</TD>
<TD width="50%">
<DIV align=right><FONT class=middlefont>Powered by: <A
href="mailto:bjchenxu@sina.com">Linux知识宝库</A> Version 0.9.0 </FONT></DIV>
</TD></TR></TBODY></TABLE>
<CENTER></CENTER></TD></TR>
</TABLE></CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -