我是一位从C#开始的新游戏开发人员.
现在我需要将我的游戏之一转移到打字稿上.
我试图用我在C#中非常熟悉的打字稿自定义列表.
我的代码如下:
export class List {
private items: Array;
constructor() {
this.items = [];
}
get count(): number {
return this.items.length;
}
add(value: T): void {
this.items.push(value);
}
get(index: number): T {
return this.items[index];
}
contains(item: T): boolean{
if(this.items.indexOf(item) != -1){
return true;
}else{
return false;
}
}
clear(){
this.items = [];
}
}
尽管如此,我还是想做一个数组,所以我可以做类似的事情:
someList[i] = this.items[i];
我想这有点像运算符重载,但我不太确定.
谁能告诉我怎么做?
提前致谢.
解决方法:
只需扩展数组
export class List<T> extends Array<T> {
constructor() {
super();
}
get count(): number {
return this.length;
}
add(value: T): void {
this.push(value);
}
get(index: number): T {
return this[index];
}
contains(item: T): boolean {
if (this.indexOf(item) != -1) {
return true;
} else {
return false;
}
}
clear() {
this.splice(0, this.count);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。