The Band چالش 6 از جلسهی 24، سخت
یک گروه موسیقی از صفر بنویس.
۱. interface Tunable با یک متد: tune(): string.
۲. کلاس abstract به اسم Instrument:
- فیلد static count از نوع number که از 0 شروع میشود.
- سازندهی کوتاه با public readonly name: string که هر بار یکی به Instrument.count اضافه میکند.
- متد abstract sound(): string.
- متد play(): string که اسم، دونقطه، یک فاصله و صدا را برمیگرداند، مثل Drum: Boom.
۳. Drum فرزند Instrument که صدایش Boom است.
۴. Guitar فرزند Instrument که قول Tunable را هم میدهد. صدایش Twang است و tune متنی مثل Guitar is tuned برمیگرداند.
برنامهی زیرش:
- const drum = new Drum("Drum"); و const guitar = new Guitar("Guitar");
- show(drum.play()); (Drum: Boom) و show(guitar.play()); (Guitar: Twang)
- show(guitar.tune()); (Guitar is tuned)
- show(Instrument.count); (2)
<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
#out { direction: ltr; text-align: left; font: 16px monospace; margin: 12px 0; padding: 12px; min-height: 28px; border-radius: 12px; background: #0f172a; color: #e2e8f0; white-space: pre-wrap; overflow-wrap: anywhere; }
#out .line { min-height: 22px; }
</style>
<pre id="out"></pre>
<script type="text/typescript">
// write Tunable, Instrument, Drum and Guitar here
// then the program
</script>
راهنما و جواب این چالش · برگشت به درس جلسهی 24: قولهای یک کلاس
بیشتر بخوان: جاوااسکریپت چیست؟ از دکمهی وب تا Node.js