跳到主要内容
版本:master

对象(object)

在GS中,对象(object)是一个包含有成员变量和方法的模块。在编写.gs文件时,实际上就是在描述一个对象的原型

object可以从文件中加载(使用load_static加载原型),也可以通过原型创建出新的object副本(使用new_object)。

与其他面向对象语言中的对象不同的是,object的变量是无法从外部访问的。开发者需要在object中提供接口函数。其他地方的程序能够访问这些函数来对object进行操作。

  • P.S.
    • 需要特别注意的是,GS的object被设计用于表达一个较大规模的、拥有较长生命周期的数据和方法集合,绝大多数情况下创建出的object并不会立即被回收。因此出于性能考虑,GC不会对object做频繁的检测,GS中的对象在创建之后 必须 手动调用destruct_object方法进行回收,否则可能存在内存的浪费,严重情况下可能导致内存资源耗尽引发的崩溃。
    • 如果需要一个类似object、不需要手动释放的“对象”,请尝试使用map或者新版本GS的class_map。

object例子简单讲解

前文中已经提到,load_static可以用于从文件中加载object的原型:

import .example;

// ...
example.xxxx(); // 这个操作也将会尝试 load_static example
object origin_obj_2 = load_static("./example.gs", this_domain());
object origin_obj_1 = load_static(example, this_domain());
// ...

若 load_static 正在首次加载原型,那么这份文件就会被编译并最终生成一个原型object,此后对相同文件进行 load_static 都将直接返回这份原型。如上代码中,由于example"./example.gs"实际上是相同的文件,因此 origin_obj_1 与 origin_obj_2 会是同一个object。


加载完对象原型之后,可以通过 new_object 函数创建一个新的对象:


object new_obj_1 = new_object(example, this_domain());
object new_obj_2 = new_object("./example.gs", this_domain());
object new_obj_3 = new_object(origin_obj_1, this_domain());

new_object 会根据传入的参数1,找到原型来创建对象,因此上述三个 new_object 创建的都是example对象。

在对象的创建(load_static & new_object)过程中,会自动地依次调用 ::entrycreate() 来对对象进行初始化操作。可以通过手动声明 create 函数的方式让对象在创建时执行一些初始化逻辑。

在对象销毁时,会调用 destruct 函数。

example.gs

void create(string msg)
{
// create 函数 *最多* 可以传入 *一个* 构造参数
// 参数在 load_static 或 new_object 时传入。
printf("On create: %s.\n", msg);
}

void destruct()
{
printf("On destruct.\n");
}

printf("On ::entry.\n");
source.gs
import .example;

load_static(example, this_domain(), "load_static()");

new_object(example, this_domain(), "new_object()");

destruct_object(example);

输出结果:

On ::entry.
On create: load_static().
On ::entry.
On create: new_object().
On destruct.

比较完整的例子:

obj.gs
int count = 0;
void create()
{
write("Create an object\n");
}

private void sub_count() // 私有函数作用:count - 1
{
count --;
write("count == ", count, "\n"); // 输出 count
}
public void add_count() // 公有函数作用:count + 1
{
count ++ ;
write("count == ", count, "\n"); // 输出 count
}

public int get_count() // 获取count的值
{
return count;
}
test.gs
object obj1 = load_static("obj.gs",this_domain());  // 此时执行create(), 输出 "Create an object", count = 0
obj1.add_count(); // count = count + 1 = 1
write(obj1.get_count()); // 输出 1
obj1.?sub_count(); // .?调用,如果函数不存在于对象也不会报错。此处因为调用私有函数,所以不执行也不报错
write(obj1.get_count()); // 还是输出 1

object常用的外部函数

下面列出object类型一些常用的外部函数以及用法。

1.
object load_static(string path, mixed domain, mixed para = nil)
加载源对象
+
2.
mixed new_object(mixed ob_or_path, mixed domain, mixed para = nil)
创建实例对象
+
3.
bool is_object(mixed val)
是否为对象
+
4.
string object_instance.name()
源文件路径
+
5.
string object_instance.dir_name()
源文件目录
+
6.
string object_instance.pure_name()
源文件名字
+
7.
ObjectState object_state(mixed val)
对象状态
+
8.
array child_objects(mixed obj)
同源对象
+
9.
bool is_static_object(mixed obj = nil)
是否源对象
+
10.
array object.get_all()
所有对象
+
11.
map object_instance.info()
对象信息
+
12.
int destruct_object(mixed ob_or_path)
销毁对象
+
13.
bool object_instance.is_destructed()
是否被销毁
+
14.
bool object_instance.is_parallel()
是否并行
+
14.
bool object_instance.is_readonly()
是否只读
+
15.
object this_object()
返回本对象
+
16.
bool object_instance.bind_post_queue(queue q)
绑定post队列
+
17.
bool object_instance.get_enable_post()
是否允许post
+
18.
void object_instance.set_enable_post(bool enable)
设置是否允许post
+
19.
handle object_instance.get_post_queue()
返回post队列
+
20.
mixed previous_object()
调用函数的对象
+
21.
void object_instance.wait_post_finish()
等待post队列函数运行结束
+
22.
array object_instance.get_components()
对象的组件
+
23.
domain object_instance.get_domain()
对象的域
+
24.
object object_instance.get_env_ob()
环境对象
+
25.
object object_instance.set_env_ob(object env_ob)
设置环境对象
+
26.
function object_instance.get_function(string name, bool can_private = false)
对象的函数
+
27.
program object_instance.get_program()
对象的program
+
28.
bool object_instance.has_component(string component_name)
是否有特定component
+
29.
bool construct_object(object oid, mixed create_para = nil)
创建对象实例
+
30.
string dump_object(object obj)
对象信息
+
31.
object find_object(string name)
特定名字的对象
+
32.
mixed object_instance.get_obj_component_var(string component_name, string var_name)
获得组件变量
+
33.
void object_instance.set_obj_component_var(string component_name, string var_name, mixed value)
设置组件变量
+

第29项用于调用new_object()函数时设置auto_contruct参数为false后,此时对象并未构造。所以需要随后调用construct_object()函数来构造对象。

表格第七项获取到的对象状态(ObjectState)包括:

ObjectState.INVALID     if value is not object type or not found the object
ObjectState.CREATING if object is creating
ObjectState.CREATED if object is created
ObjectState.DESTRUCTING if object is destructing
ObjectState.DESTRUCTED if object has been destructed

第21项例子

obj.gs
int count = 0;

public void add_count() // 公有函数作用:count + 1
{
count ++ ;
write("count == ", count, "\n"); // 输出 count
}

public void show_count() // 显示count的值
{
write("count == ", count, "\n");
}
main.gs
object obj = load_static("/obj.gs", this_domain());
@obj.show_count();
@obj.add_count();
obj.wait_post_finish();

write("program finish\n"); // 先执行show_count()和add_count(),然后输出"program finish"