我正在尝试使用typescript
的课程方法。这个对我有用。但是我在“宋”的建设中得到了强调。任何人都可以解释我的问题是什么?
而不是仅仅纠正我的代码,我在这里寻找正确的理解。
这是我的代码:
class Song {
artist:string, title:string - both underlined
constructor( artist:string, title:string ) {}
play() {
console.log( 'Playing' + this.title + ' by' + this.artist );
}
}
var songs = [
new Song('Bushbaby', 'Megaphone'),
new Song('Delays', 'One More Lie In'),
new Song('Goober Gun', 'Stereo'),
new Song('Sohnee', 'Shatter'),
new Song('Get Amped', 'Celebrity')
];
class Jukebox {
constructor(private songs: Song[]) { };
run () {
var song = this.getRandomSong();
song.play();
};
private getRandomSong () {
var songCount = this.songs.length;
var songIndex = Math.floor(Math.random() * songCount);
return this.songs[songIndex];
};
}
var jukebox = new Jukebox(songs);
jukebox.run();