【发布时间】:2017-08-26 02:21:05
【问题描述】:
我有一些艺术品想要放入数组中。每件艺术品都有我试图用一个类来描述的属性。我想用类创建类型并将数据推送到数组中。我也不确定构造函数在做什么?我认为它应该提供一种创建类的新实例的方法?
我的ts代码如下:
class Artwork {
private _artTitle: string;
private _slideUrl: string;
private _artPrice: string;
private _artMedium: string;
private _artDimensions: string;
private _artDesc: string;
constructor(
artTitle: string,
slideUrl: string,
artPrice: string,
artMedium: string,
artDimensions: string,
artDesc: string
){
this._artTitle = artTitle;
this._slideUrl = slideUrl;
this._artPrice = artPrice;
this._artMedium = artMedium;
this._artDimensions = artDimensions;
this._artDesc = artDesc;
}
}
let Artwks: string [];
Artwks.push(new Artwork());
Artwks[0].slideUrl = './assets/SBI_Slide_1.jpg';
Artwks[0].artTitle = "'Surprising Breakfast Ideas'";
Artwks[0].artPrice = "£400";
Artwks[0].artMedium = "Acrylic on canvas";
Artwks[0].artDimensions = '7" x 12"';
Artwks[0].artDesc = '...Inspired by trailing clauses and footling concerns, this latest injunction into the darkened forest of prevarication yields a miasma of plethoras, tantalising us with a fleeting array of nuances...';
console.log(Artwks);
【问题讨论】:
-
仅供参考,您可以在构造函数中将某些内容定义为
private/protected/public,然后访问它,而无需单独声明类变量,然后将构造函数中的值分配给它们。例如。constructor(private _artTitle: string)
标签: arrays class typescript