泛型
通用参数在运行时不可用,它们仅用于编译时。这意味着你不能做这样的事情:
class Executor<T, U> {
public execute(executable: T): void {
if (T instanceof Executable1) { // Compilation error
...
} else if (U instanceof Executable2){ // Compilation error
...
}
}
}
但是,类信息仍然保留,因此你仍然可以测试变量的类型,因为你始终能够:
class Executor<T, U> {
public execute(executable: T): void {
if (executable instanceof Executable1) {
...
} else if (executable instanceof Executable2){
...
} // But in this method, since there is no parameter of type `U` it is non-sensical to ask about U's "type"
}
}