typescript2.0 - 有没有更好的方法来制作 setter 非原始用户定义类型属性?
问题描述
嗨,我是打字稿的新手。我在以下示例中总结了我的用例。
class Movie {
private crew: Crew;
private cast: Cast;
private locations: Location;
private movieId;
constructor(public id: string) {
this.movieId = id;
}
public getMovieId() {
return this.moveId;
}
public setCrew(value: Crew){
this.crew = value;
}
public getCrew() {
return this.crew;
}
...
//other getter and setters.
};
type Crew = {
crew_varA: string,
crew_varB: boolean,
crew_varC: string,
crew_varD: boolean,
}
type Cast = {
cast_varE: string,
cast_varF: string,
cast_varG: string,
}
type Location = {
loc_varX: string,
loc_varY: string,
loc_varZ: string,
}
当我创建一个 Movie 对象时,我需要调用 setCrew() 方法。但到那时,只有“crew_varA”变量才有价值。我无法仅使用一个变量调用 setCrew() 方法。我需要用一些不合适的默认值初始化所有变量。
let mv = new Movie("SpiderMan");
mv.setCrew({crew_varA: "Peter Parker", crew_varB:false, crew_varC:null, crew_varD:null}; //is there a way to set only one property of Crew and add other properties one by one based on some logic??
我不确定我的方法是否正确。我感谢您的帮助。
解决方案
推荐阅读
- css - 之前不要旋转标签
- ios - 如何在折线图中的 X 轴和 Y 轴上给出点?
- list - 在 Haskell 中解决递归问题后出现警告
- file-upload - TYPO3:如何使用文件格式限制 Powermail 中的文件上传
- amazon-cognito - getUserData 在页面刷新或 componentDidMount 时返回旧的 UserData
- redis - Redis Geoadd 没有按照用户存储准确的纬度和经度输入
- filter - 如何使文件选择器对话框返回短路径?
- perl - 如何使用 Perl 以分钟为单位获取日期差异
- java - 如何让 EventSource.onmessage 工作?
- django - django ORM 聚合计数