How JScript Overloads Methods
当两个以上的JScript成员在一个类中有同名但不同签名,这就要提到重载函数"overloaded"(properties).这里的一个函数的"signature(签名)"是又参数的个数,类型和顺序来决定.如果他们确定两个函数在同样的参数顺序中有同样的参数个数,同样的类型,他们这两个函数有相同的签名.函数确定在不同的参数顺序中有同样的类型参数,或函数有在不同的参数顺序中有同样的参数个数.他们都不是相同的函数签名.(注意:参数的名字对函数签名没有影响).Static函数也会参与重载,但如返回类型,在一个方法的在static状态中就不会影响它的函数签名.之前一个static方法同名,就像一个实例方法必须有一个不同的参数列表.
Processing Logic
当一个已经重载的函数被调用,已重载的函数的会调用相应的参数,依赖的是函数参数类型.如果参数类型匹配重载法则,重载就会被调用,如果参数类型不匹配任何重载法则,一个清除处理决定重载的调用.这个清出处理是基本就是怎样能容易将当去前的类型转换成能重载的类型!详细可以看Coercion in JScript.例如类的MethodOverload已经有三个重载方法名为Greetings, 首先overload 没有碍参数,第二有两个参数,第三个有三个参数.
Processing Logic
当一个已经重载的函数被调用,已重载的函数的会调用相应的参数,依赖的是函数参数类型.如果参数类型匹配重载法则,重载就会被调用,如果参数类型不匹配任何重载法则,一个清除处理决定重载的调用.这个清出处理是基本就是怎样能容易将当去前的类型转换成能重载的类型!详细可以看Coercion in JScript.例如类的MethodOverload已经有三个重载方法名为Greetings, 首先overload 没有碍参数,第二有两个参数,第三个有三个参数.
var methodOverload = new MethodOverload();
methodOverload.Greetings();
methodOverload.Greetings("Mr. Brown");
methodOverload.Greetings(97, "Mr. Brown");
class MethodOverload
{
function Greetings()
{
print("Hello, and welcome!");
}
function Greetings(name : String)
{
print("Hello, " + name + "!");
}
function Greetings(ticket : int, name : String)
{
print("Hello, " + name + "! Your ticket number is " + ticket + ".");
}
}
输出的结果是:methodOverload.Greetings();
methodOverload.Greetings("Mr. Brown");
methodOverload.Greetings(97, "Mr. Brown");
class MethodOverload
{
function Greetings()
{
print("Hello, and welcome!");
}
function Greetings(name : String)
{
print("Hello, " + name + "!");
}
function Greetings(ticket : int, name : String)
{
print("Hello, " + name + "! Your ticket number is " + ticket + ".");
}
}
Hello, and welcome!
Hello, Mr.Brown!
Hello, Mr.Brown! Your ticket number is 97.
Hello, Mr.Brown!
Hello, Mr.Brown! Your ticket number is 97.