先带来日常的 GScript 更新:新增了可变参数的特性,语法如下:
int add(string s, int ...num){
println(s);
int sum = 0;
for(int i=0;i得益于可变参数,所以新增了格式化字符串的内置函数:
//formats according to a format specifier and writes to standard output.
printf(string format, any ...a){}
//formats according to a format specifier and returns the resulting string.
string sprintf(string format, any ...a){}
下面重点看看 GScript所支持的运算符重载是如何实现的。
运算符重载其实也是多态的一种表现形式,我们可以重写运算符的重载函数,从而改变他们的计算规则。
println(100+2*2);
以这段代码的运算符为例,输出的结果自然是:104.
但如果我们是对两个对象进行计算呢,举个例子:
class Person{
int age;
Person(int a){
age = a;
}