对象(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)过程中,会自动地依次调用 ::entry
和 create()
来对对象进行初始化操作。可以通过手动声明 create
函数的方式让对象在创建时执行一些初始化逻辑。
在对象销毁时,会调用 destruct
函数。
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");
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.
比较完整的例子:
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;
}
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类型一些常用的外部函数以及用法。
object load_static(string path, mixed domain, mixed para = nil)
mixed new_object(mixed ob_or_path, mixed domain, mixed para = nil)
bool is_object(mixed val)
string object_instance.name()
string object_instance.dir_name()
string object_instance.pure_name()
ObjectState object_state(mixed val)
array child_objects(mixed obj)
bool is_static_object(mixed obj = nil)
array object.get_all()
map object_instance.info()
int destruct_object(mixed ob_or_path)
bool object_instance.is_destructed()
bool object_instance.is_parallel()
bool object_instance.is_readonly()
object this_object()
bool object_instance.bind_post_queue(queue q)
bool object_instance.get_enable_post()
void object_instance.set_enable_post(bool enable)
handle object_instance.get_post_queue()
mixed previous_object()
void object_instance.wait_post_finish()
array object_instance.get_components()
domain object_instance.get_domain()
object object_instance.get_env_ob()
object object_instance.set_env_ob(object env_ob)
function object_instance.get_function(string name, bool can_private = false)
program object_instance.get_program()
bool object_instance.has_component(string component_name)
bool construct_object(object oid, mixed create_para = nil)
string dump_object(object obj)
object find_object(string name)
mixed object_instance.get_obj_component_var(string component_name, string var_name)
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项例子
int count = 0;
public void add_count() // 公有函数作用:count + 1
{
count ++ ;
write("count == ", count, "\n"); // 输出 count
}
public void show_count() // 显示count的值
{
write("count == ", count, "\n");
}
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"