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

جلسه‌ی 24: قول‌های یک کلاس Promises of a Class

یاد می‌گیری کلاس قول بدهد: با implements قول می‌دهد شکل یک interface را داشته باشد، با abstract والدی می‌سازی که فقط یک ایده است و فرزندانش باید کاملش کنند، و با static چیزی می‌سازی که مال خود کلاس است، نه تک‌تک شیءها.

در این جلسه یاد می‌گیری

درس

در بازی رضا هر چیزی که روی صفحه می‌آید باید متد draw داشته باشد. او این را در یک interface نوشته است: Drawable. بعد کلاس Star را ساخت، ولی اسم متد را اشتباه نوشت: drow. کلاس هیچ خط قرمزی ندارد، چون از نظر خودش یک کلاس سالم است. خط قرمز خیلی دورتر پیدا می‌شود: جایی که ستاره را به تابع paint می‌دهند. اجرا کن و ببین خط قرمز کجا افتاده است. در یک برنامه‌ی بزرگ، آن‌جا ممکن است صد خط پایین‌تر باشد.

<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">
interface Drawable {
  draw(): string;
}

class Star {
  drow(): string {
    return "*";
  }
}

function paint(thing: Drawable): void {
  show(thing.draw());
}

paint(new Star());
</script>

بهتر است کلاس از همان اول قول بدهد. class Star implements Drawable یعنی: «قول می‌دهم هر چه در Drawable نوشته شده، داشته باشم.» حالا تایپ‌اسکریپت خودِ کلاس را با قولش مقایسه می‌کند. این‌جا قول درست انجام شده و برنامه اجرا می‌شود.

<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">
interface Drawable {
  draw(): string;
}

class Star implements Drawable {
  draw(): string {
    return "*";
  }
}

function paint(thing: Drawable): void {
  show(thing.draw());
}

paint(new Star());
</script>

حالا همان غلط املایی، ولی این بار با قول. اجرا کن: خط قرمز روی خود کلاس است، درست همان‌جا که اشتباه شده. پیامش هم روشن است: Star قول Drawable را داده، ولی draw ندارد. این فایده‌ی implements است: اشتباه همان‌جایی پیدا می‌شود که ساخته شده.

<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">
interface Drawable {
  draw(): string;
}

class Star implements Drawable {
  drow(): string {
    return "*";
  }
}

show(new Star().drow());
</script>

فرق extends و implements را خوب یاد بگیر. با extends از والد کد می‌گیری: فیلدها و متدهای آماده. با implements هیچ کدی نمی‌گیری، فقط قول می‌دهی و همه چیز را خودت می‌نویسی. یک کلاس فقط یک والد دارد، ولی می‌تواند چند قول با هم بدهد: implements Drawable, Scorable.

<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">
interface Drawable {
  draw(): string;
}

interface Scorable {
  points(): number;
}

class Coin implements Drawable, Scorable {
  draw(): string {
    return "o";
  }

  points(): number {
    return 10;
  }
}

const coin = new Coin();
show(coin.draw(), coin.points());
</script>

کلاس Shape جلسه‌ی قبل را یادت هست؟ area در آن 0 برمی‌گرداند، که یک دروغ کوچک بود: «شکلِ خالی» اصلاً وجود ندارد. فقط مربع و مستطیل وجود دارند. برای چنین والدی abstract داریم: کلاسی که فقط یک ایده است. متدِ abstract بدنه ندارد، فقط می‌گوید: «هر فرزند باید این را بنویسد.» بقیه‌ی متدها، مثل describe، معمولی‌اند و به فرزندان می‌رسند.

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

  abstract area(): number;

  describe(): string {
    return this.name + " area " + this.area();
  }
}

class Square extends Shape {
  constructor(private side: number) {
    super("Square");
  }

  area(): number {
    return this.side * this.side;
  }
}

show(new Square(4).describe());
</script>

از یک ایده نمی‌شود چیزی ساخت. اجرا کن: new Shape(...) خط قرمز است، چون Shape یک کلاس abstract است. خط قرمز دوم هم داریم: اگر فرزندی area را ننویسد، روی همان فرزند خط قرمز می‌افتد. یعنی دیگر هیچ شکلی نمی‌تواند مساحتش را فراموش کند.

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

  abstract area(): number;
}

class Circle extends Shape {}

const thing = new Shape("Thing");
show(thing.area());
</script>

آخرین کلمه: static. تا حالا هر فیلد مال یک شیء بود: هر بازیکن امتیاز خودش را دارد. ولی «تا حالا چند بازیکن ساخته شده؟» مال هیچ بازیکنی نیست. مال خود کلاس است. چیزِ static را با اسم کلاس صدا می‌زنی، نه با this: Player.count. متد هم می‌تواند static باشد: ابزاری که به هیچ شیئی نیاز ندارد.

<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 Player {
  static count: number = 0;

  constructor(public name: string) {
    Player.count = Player.count + 1;
  }

  static welcome(name: string): string {
    return "Welcome, " + name + "!";
  }
}

const sara = new Player("Sara");
const ali = new Player("Ali");
show(Player.count);
show(Player.welcome(ali.name));
</script>

خلاصه در سه سؤال. کدِ مشترک دارم و می‌خواهم یک بار نوشته شود؟ extends. می‌خواهم مطمئن شوم کلاسم شکل خاصی دارد؟ implements. والدم فقط یک ایده است و هر فرزند باید بخشی را خودش بنویسد؟ abstract. یک اشتباه رایج هم: sara.count یا this.count برای چیز static خط قرمز است. درستش Player.count است.

مثال‌های این جلسه

چالش‌ها

  1. Keep the Promise (آسان)
  2. No Object Needed (آسان)
  3. Two Real Shapes (متوسط)
  4. Anything You Can Turn On (متوسط)
  5. Find the Red Lines (سخت)
  6. The Band (سخت)

بیشتر بخوان

← جلسه‌ی 23: خانواده‌ها جلسه‌ی 25: پروژه: قهرمان‌ها و هیولاها →