Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript nodejs转换中的java代码到typescript_Javascript_Java_Typescript_Generics - Fatal编程技术网

Javascript nodejs转换中的java代码到typescript

Javascript nodejs转换中的java代码到typescript,javascript,java,typescript,generics,Javascript,Java,Typescript,Generics,我想将这个java类转换为nodejs,但在理解泛型的概念上有问题。 请描述零件泛型,并帮助我转换为nodejs typescript 编辑状态类: import java.util.ArrayList; import java.util.List; public class EditorState{ private final String content; public EditorState(String content){ this.content = content; }

我想将这个java类转换为nodejs,但在理解泛型的概念上有问题。
请描述零件泛型,并帮助我转换为nodejs typescript

编辑状态类:

import java.util.ArrayList;
import java.util.List;

public class EditorState{
private final String content;

public EditorState(String content){
    this.content = content;
}
public String getContent(){
    return content;
}
}
历史课:

  public class History {

  private List<EditorState> states = new ArrayList<>();
  
  public void push(EditorState state){
     state.add(state);
  }  
  }
公共类历史记录{
私有列表状态=新的ArrayList();
公共无效推送(EditorState状态){
state.add(state);
}  
}

泛型类型不是javascript固有的,在运行时完全被忽略。在TypeScript中,它们是在开发过程中识别对象的一种很好的方法。例如,您可能已经知道的一个泛型是Array,或string[],它是一个字符串数组。使用泛型在typescript中翻译的java代码可能如下所示:

public class History<T> {
    private states:T[] = new Array// Array of T 
    public push(state:T):void {
        this.states.push(state) // assuming code you gave was a typo
    }
}
public class EditorState {
    private content:string
    constructor(content:string) {
        this.content = content
    }
    public getContent():string {
        return this.content
    }
}

//Usage

let hist:History<EditorState> = new History
let state = new EditorState("Something")
hist.push(state) //this is OK, as T is EditorState, and state is an Instance of EditorState
hist.push("Hello World") //This will raise an Error, as String is not of type EditorState
公共类历史记录{
私有状态:T[]=新数组//T的数组
公共推送(状态:T):无效{
this.states.push(state)//假设您给出的代码是一个输入错误
}
}
公共类编辑状态{
私有内容:字符串
构造函数(内容:字符串){
this.content=content
}
public getContent():字符串{
返回此.content
}
}
//用法
让hist:History=新历史
让状态=新编辑器状态(“某物”)
hist.push(state)//这是可以的,因为T是EditorState,state是EditorState的实例
hist.push(“Hello World”)//这将引发错误,因为字符串不是EditorState类型
这里T用作泛型类型变量,当我们创建
hist

请注意,泛型类型仅在开发中,并且从运行时执行的代码中删除,但在开发过程中,将强制执行类型。因此,尝试向history.push推送字符串值将在编译时出错


关于泛型的更多信息,有一些很好的文档可供参考:

你将度过一段非常糟糕的时光。发布的代码是Java。Java与JavaScript无关。Typescript与JavaScript相关。我知道我想将这些类转换为Typescript。这就是为什么你会很不开心的原因。Java与typescript和javascript完全无关。我知道你的意思,它只是一些设计模式的示例代码,我想在nodejs的typescript中理解它们,而不是一个项目。是一个可能的打字脚本代码。谢谢,这就是我要找的