بتل کد Battle Codeدارد باز می‌شود…

جواب «Find the Red Lines» چالش 5 از جلسه‌ی 23

سؤال

CookBot یک ربات آشپز است، فرزند Robot. برنامه باید بنویسد:
I am Chef, I cook soup و در خط بعد soup
ولی سه اشتباه دارد (یکی از آن‌ها دو خط قرمز می‌سازد):
- سازنده‌ی فرزند والدش را نساخته است.
- متد hello در فرزند نوع خروجی‌اش را عوض کرده، که اجازه ندارد.
- جعبه‌ی bot با نوع والد ساخته شده، ولی برنامه از آن چیزی می‌خواهد که فقط فرزند دارد.

راهنما

داخل سازنده بنویس super(name);. hello(): string. و const bot: CookBot = ... (یا نوع را ننویس تا خودش حدس بزند).

یک جواب درست

<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">
class Robot {
  constructor(public name: string) {}

  hello(): string {
    return "I am " + this.name;
  }
}

class CookBot extends Robot {
  constructor(name: string, public dish: string) {
    super(name);
  }

  hello(): string {
    return super.hello() + ", I cook " + this.dish;
  }
}

const bot: CookBot = new CookBot("Chef", "soup");
show(bot.hello());
show(bot.dish);
</script>

اول خودت امتحان کن؛ جواب‌های دیگر هم قبول است. برگرد و خودت حلش کن