gpt4 book ai didi

javascript - 如何在父类(super class)构造函数中初始化/设置值?在 typescript 中

转载 作者:行者123 更新时间:2023-12-01 01:25:39 25 4
gpt4 key购买 nike

给定这两个类:

用户

    export class User {

constructor(id?: string, userName?: string, fullName?: string,
email?: string, jobTitle?: string, phoneNumber?: string, roles?: string[]) {
this.id = id;
this.userName = userName;
this.fullName = fullName;
this.email = email;
this.jobTitle = jobTitle;
this.phoneNumber = phoneNumber;
this.roles = roles;
}

}

和从User扩展UserEdit

import { User } from './user.model';


export class UserEdit extends User {
constructor(currentPassword?: string, newPassword?: string, confirmPassword?: string) {
super();

this.currentPassword = currentPassword;
this.newPassword = newPassword;
this.confirmPassword = confirmPassword;
}

public currentPassword: string;
public newPassword: string;
public confirmPassword: string;

}

可以从UserEditemail设置为空字符串'',从创建新对象?。例如。

let userEdit:UserEdit = new UserEdit()

最佳答案

您需要使用 super(param1, param2, param3, ...) 在基类中传递参数

就你的情况

super(id, userName, fullName, email, jobTitle, phoneNumber, roles);

super(id, username, fullname, '', jobTitle, phoneNumber, roles);

关于javascript - 如何在父类(super class)构造函数中初始化/设置值?在 typescript 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53823637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com