如何修复从Java转换后的类型脚本类文件错误?

我有一个Java类,我想在类型脚本项目中使用的哪个类。但我试图转换它,并得到了 http://www.jsweet.org/jsweet-live-sandbox/ 的帮助。我对类型脚本非常陌生,我是一名Java开发人员,目前正在学习前端语言。所以我面临着识别错误的问题。如果您帮助我修复我的类型脚本代码,那将非常有帮助。


这是我的 Java 类:


import bd.edu.seu.erp.model.exception.IncorrectSemesterError;


import java.util.Arrays;

import java.util.HashMap;

import java.util.Map;


public class Semester {

    private static final String NAMES[] = {"Spring", "Summer", "Fall"};

    private static final int OFFSET = 2002;


    private static Map<Integer, Semester> semesterMap = new HashMap<>();

    private static Map<String, Semester> semesterNameMap = new HashMap<>();


    private int semesterNumber;

    private String label;


    public Semester(int semesterNumber, String label) {

        this.semesterNumber = semesterNumber;

        this.label = label;

    }


    private Semester(int semesterNumber) {

        if (semesterNumber < 1)

            throw new IncorrectSemesterError("Invalid Semester", "Semester number cannot be less than 1");

        this.semesterNumber = semesterNumber;

        this.label = NAMES[semesterNumber % NAMES.length] + " " + (semesterNumber / 3 + OFFSET);


    }


首先,我在类型脚本构造函数中感到困惑。我不确定,这是我在java中为多个构造函数编写的正确方法吗?其次,在打字稿中,我没有找到“或”默认值“,在”地图“中放置”。我不明白这里应该写些什么。


你能帮我修复这个打字机代码吗?提前致谢。


长风秋雁
浏览 112回答 2
2回答

杨魅力

我也有Java背景,但年龄较大。;-)一些建议:选择或,但您不应同时使用两者。在下面的示例中,我使用undefinednullundefined;在模块中,静态成员可以用简单的变量替换;优先于多个可选参数;interface您的IIFE()是无用的;(() => { … }();当推理就足够了时,不要声明类型;首选基元类型 ( 而不是 )。stringString下面是一个示例:const NAMES = ['Spring', 'Summer', 'Fall'];const OFFSET = 2002;const numberMap = new Map<number, Semester>();const labelMap = new Map<string, Semester>();export interface SemesterOptions {&nbsp; &nbsp; semesterNumber?: number&nbsp; &nbsp; label?: string}export class Semester {&nbsp; &nbsp; private semesterNumber: number;&nbsp; &nbsp; private label: string;&nbsp; &nbsp; constructor({ semesterNumber, label }: SemesterOptions) {&nbsp; &nbsp; &nbsp; &nbsp; if (semesterNumber === undefined) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (label === undefined) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new Error('invalid options');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const parsed = parseLabelToSemesterOptions(label);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; semesterNumber = parsed.semesterNumber;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label = parsed.label;&nbsp; &nbsp; &nbsp; &nbsp; } else if (label === undefined) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (semesterNumber < 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new Error('Semester number cannot be less than 1');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label = NAMES[semesterNumber % NAMES.length] + ' ' + (semesterNumber / 3 + OFFSET);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; this.semesterNumber = semesterNumber;&nbsp; &nbsp; &nbsp; &nbsp; this.label = label;&nbsp; &nbsp; }&nbsp; &nbsp; static ofNumber(semesterNumber: number): Semester {&nbsp; &nbsp; &nbsp; &nbsp; let semester = numberMap.get(semesterNumber);&nbsp; &nbsp; &nbsp; &nbsp; if (!semester) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; semester = new Semester({ semesterNumber });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; numberMap.set(semester.semesterNumber, semester);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!labelMap.has(semester.label)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; labelMap.set(semester.label, semester);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return semester;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; static ofLabel(label: string): Semester {&nbsp; &nbsp; &nbsp; &nbsp; let semester = labelMap.get(label);&nbsp; &nbsp; &nbsp; &nbsp; if (!semester) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; semester = new Semester({ label });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; labelMap.set(semester.label, semester);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!numberMap.has(semester.semesterNumber)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; numberMap.set(semester.semesterNumber, semester);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return semester;&nbsp;&nbsp; &nbsp; }}function parseLabelToSemesterOptions(labelToParse: string): Required<SemesterOptions> {&nbsp; &nbsp; const tokens = labelToParse.split('\\ ');&nbsp; &nbsp; if (tokens.length !== 2) {&nbsp; &nbsp; &nbsp; &nbsp; throw new Error('Semester label has incorrect number of tokens');&nbsp; &nbsp; }&nbsp; &nbsp; const label = tokens[0];&nbsp; &nbsp; const year = parseInt(tokens[1], 10);&nbsp; &nbsp; if (year < OFFSET) {&nbsp; &nbsp; &nbsp; &nbsp; throw new Error('Year cannot be earlier than ' + OFFSET);&nbsp; &nbsp; }&nbsp; &nbsp; const nameIndex = NAMES.indexOf(label);&nbsp; &nbsp; if (nameIndex === -1) {&nbsp; &nbsp; &nbsp; &nbsp; throw new Error(`Name of the semester must be one of ${NAMES.join(', ')}`);&nbsp; &nbsp; }&nbsp; &nbsp; const semesterNumber = (year - OFFSET) * 3 + nameIndex;&nbsp; &nbsp; return {&nbsp; &nbsp; &nbsp; &nbsp; semesterNumber,&nbsp; &nbsp; &nbsp; &nbsp; label&nbsp; &nbsp; }}对于 :使用获取并测试结果是否为(或错误)。有一个带有测试的快捷方式:getOrDefaultundefinedfalsyconst val = myMap.get(myKey) || myDefautValue;...但在你的情况下,声明更合适。if对于 :,首先使用测试密钥是否已存在,然后在必要时使用 set。putIfAbsent

慕虎7371278

关于构造函数你猜对了,没有像你在Java中习惯的那样有多个构造函数(又名构造函数重载)。TypeScript 中只有一个构造函数,并且需要有可选参数。在您的情况下,学期编号和标签可以是未定义的或数字/字符串,无需进行类型检查。检查“传入的内容”的方法不需要在一个if语句中进行如此多的检查。在这里看看下面的答案:https://stackoverflow.com/a/44017547/8745384关于地图“putIfAb”的等价物是一个简单的“设置(键,值)”Semester.semesterMap.set(semester.semesterNumber,&nbsp;semester);对于获取或默认设置,您可以编写const&nbsp;semester:&nbsp;Semester&nbsp;=&nbsp;Semester.semesterMap.get(semesterNumber)&nbsp;||&nbsp;new&nbsp;Semester(semesterNumber);它的作用与我之前对if语句的描述相同。检查空值或未定义或空(如果是这种情况)执行新学期(...)。希望我能帮忙。我最近也从 Java 切换到了类型脚本,所以也许有一种比我描述的更好的方法。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java