静态函数特点

  1. 静态函数书写于类内部;
  2. 静态函数可以在类没有实例化和初始化为对象时直接使用调用,用法是:类名.静态方法名();
  3. 声明时在func关键字前加上static关键字即可;
  4. 静态函数不能访问类中的成员变量,除非是静态变量,但是GDS的类目前不支持静态变量;
  5. 静态函数的用途:用类文件封装属于自己的函数库。

    第一个静态函数库file

    首先静态函数库应当是一个类文件,需要用class_name关键字为它设定一个独一无二的名称,然后书写封装好的函数,记得func之前加上 static关键字。 ```python

    =======================================================

    关于文件操作的静态函数库

    =======================================================

class_name file

保存字符串到指定路径的文件

static func save(path:String,content:String): var file = File.new() if file.file_exists(path): file.open(path,File.WRITE) file.store_string(content) file.close()

返回指令路径文件中的内容

static func loadString(path:String): var file = File.new() var string = “” if file.file_exists(path): file.open(path,File.READ) string = file.get_as_text() file.close() return string ``` 保存之后,就可以在其他脚本,包括Autoload的脚本中通过“类名.函数名”的方式调用方法。