|
62 | 62 | }, |
63 | 63 | { |
64 | 64 | "cell_type": "code", |
65 | | - "execution_count": 2, |
| 65 | + "execution_count": 1, |
66 | 66 | "id": "8a3b104b", |
67 | 67 | "metadata": {}, |
68 | 68 | "outputs": [], |
|
89 | 89 | }, |
90 | 90 | { |
91 | 91 | "cell_type": "code", |
92 | | - "execution_count": 3, |
| 92 | + "execution_count": 2, |
93 | 93 | "id": "70a3e601", |
94 | 94 | "metadata": {}, |
95 | 95 | "outputs": [], |
|
108 | 108 | }, |
109 | 109 | { |
110 | 110 | "cell_type": "code", |
111 | | - "execution_count": 4, |
| 111 | + "execution_count": 3, |
112 | 112 | "id": "4355b15b", |
113 | 113 | "metadata": {}, |
114 | 114 | "outputs": [ |
|
155 | 155 | }, |
156 | 156 | { |
157 | 157 | "cell_type": "code", |
158 | | - "execution_count": 16, |
| 158 | + "execution_count": 4, |
159 | 159 | "id": "117bef29", |
160 | 160 | "metadata": {}, |
161 | 161 | "outputs": [], |
|
191 | 191 | }, |
192 | 192 | { |
193 | 193 | "cell_type": "code", |
194 | | - "execution_count": 17, |
| 194 | + "execution_count": 5, |
195 | 195 | "id": "53a5fb9f", |
196 | 196 | "metadata": {}, |
197 | 197 | "outputs": [ |
|
284 | 284 | }, |
285 | 285 | { |
286 | 286 | "cell_type": "code", |
287 | | - "execution_count": 18, |
| 287 | + "execution_count": 6, |
288 | 288 | "id": "65a01513", |
289 | 289 | "metadata": {}, |
290 | 290 | "outputs": [], |
|
314 | 314 | }, |
315 | 315 | { |
316 | 316 | "cell_type": "code", |
317 | | - "execution_count": 19, |
| 317 | + "execution_count": 7, |
318 | 318 | "id": "6dcd2d8d", |
319 | 319 | "metadata": {}, |
320 | 320 | "outputs": [ |
|
324 | 324 | "(True, True)" |
325 | 325 | ] |
326 | 326 | }, |
327 | | - "execution_count": 19, |
| 327 | + "execution_count": 7, |
328 | 328 | "metadata": {}, |
329 | 329 | "output_type": "execute_result" |
330 | 330 | } |
|
345 | 345 | }, |
346 | 346 | { |
347 | 347 | "cell_type": "code", |
348 | | - "execution_count": 20, |
| 348 | + "execution_count": 8, |
349 | 349 | "id": "940fdf90", |
350 | 350 | "metadata": {}, |
351 | 351 | "outputs": [ |
|
374 | 374 | }, |
375 | 375 | { |
376 | 376 | "cell_type": "code", |
377 | | - "execution_count": 21, |
| 377 | + "execution_count": 9, |
378 | 378 | "id": "ceeaf5d4", |
379 | 379 | "metadata": {}, |
380 | 380 | "outputs": [ |
|
395 | 395 | " print(\"일반 사람은 attack() 메서드를 갖고 있지 않습니다.\")" |
396 | 396 | ] |
397 | 397 | }, |
| 398 | + { |
| 399 | + "cell_type": "markdown", |
| 400 | + "metadata": {}, |
| 401 | + "source": [ |
| 402 | + "`park`은 `Person` 클래스의 객체이기는 하지만 `Hero` 클래스의 객체는 아니다." |
| 403 | + ] |
| 404 | + }, |
| 405 | + { |
| 406 | + "cell_type": "code", |
| 407 | + "execution_count": 10, |
| 408 | + "metadata": {}, |
| 409 | + "outputs": [ |
| 410 | + { |
| 411 | + "data": { |
| 412 | + "text/plain": [ |
| 413 | + "(True, False)" |
| 414 | + ] |
| 415 | + }, |
| 416 | + "execution_count": 10, |
| 417 | + "metadata": {}, |
| 418 | + "output_type": "execute_result" |
| 419 | + } |
| 420 | + ], |
| 421 | + "source": [ |
| 422 | + "isinstance(park, Person), isinstance(park, Hero)" |
| 423 | + ] |
| 424 | + }, |
398 | 425 | { |
399 | 426 | "cell_type": "markdown", |
400 | 427 | "id": "a82e686d", |
|
425 | 452 | "그런 다음 `Hero`의 `__init__()` 메서드는 영웅에게만 필요한\n", |
426 | 453 | "`power`, `health`, `damage`, `inventory` 속성을 추가로 초기화한다.\n", |
427 | 454 | "\n", |
428 | | - "이처럼 부모 클래스에서 상속받은 메서드를 자식 클래스에서 같은 이름으로 다시 정의하는 것을\n", |
| 455 | + "**메서드 오버라이딩**\n", |
| 456 | + "\n", |
| 457 | + "부모 클래스에서 상속받은 메서드를 자식 클래스에서 같은 이름으로 다시 정의하는 것을\n", |
429 | 458 | "**메서드 오버라이딩**<font size='2'>method overriding</font>이라고 한다.\n", |
430 | 459 | "\n", |
431 | | - "예를 들어 `Hero`의 `introduction()` 메서드를 재정의하여\n", |
432 | | - "일반적인 자기소개에 파워와 무기 정보를 추가할 수 있다." |
| 460 | + "아래 `Hero` 클래스는 부모 클래스인 `Person` 클래스로부터 상속받은 `introduction()` 메서드도 재정의하여\n", |
| 461 | + "일반적인 자기소개와 함께 파워와 무기 정보도 제공한다." |
433 | 462 | ] |
434 | 463 | }, |
435 | 464 | { |
436 | 465 | "cell_type": "code", |
437 | | - "execution_count": 22, |
| 466 | + "execution_count": 11, |
438 | 467 | "id": "9e0e9310", |
439 | 468 | "metadata": {}, |
440 | 469 | "outputs": [], |
|
460 | 489 | }, |
461 | 490 | { |
462 | 491 | "cell_type": "code", |
463 | | - "execution_count": 23, |
| 492 | + "execution_count": 12, |
464 | 493 | "id": "22a3cf8a", |
465 | 494 | "metadata": {}, |
466 | 495 | "outputs": [ |
|
487 | 516 | "print(\"헐크 체력:\", hulk.health)" |
488 | 517 | ] |
489 | 518 | }, |
490 | | - { |
491 | | - "cell_type": "markdown", |
492 | | - "id": "b748f3c9", |
493 | | - "metadata": {}, |
494 | | - "source": [ |
495 | | - "### 상속과 `*args`, `**kwargs` 매개변수 활용" |
496 | | - ] |
497 | | - }, |
498 | | - { |
499 | | - "cell_type": "markdown", |
500 | | - "id": "6ea5a2c9", |
501 | | - "metadata": {}, |
502 | | - "source": [ |
503 | | - "`*args`와 `**kwargs`는 함수를 정의할 때 인자의 수를 미리 지정하지 않을 때 사용하는 표현식이다.\n", |
504 | | - "\n", |
505 | | - "- `*args`: 임의의 개수의 위치 인자를 의미함\n", |
506 | | - "- `**kwargs`: 임의의 개수의 키워드 인자를 의미함\n", |
507 | | - "\n", |
508 | | - "이 방식을 이용하면 자식 클래스의 `__init__()` 메서드에서 부모 클래스의 초기 설정 인자를\n", |
509 | | - "그대로 전달할 수 있다." |
510 | | - ] |
511 | | - }, |
512 | | - { |
513 | | - "cell_type": "code", |
514 | | - "execution_count": 25, |
515 | | - "id": "bfcf6f80", |
516 | | - "metadata": {}, |
517 | | - "outputs": [], |
518 | | - "source": [ |
519 | | - "class Hero(Person):\n", |
520 | | - " def __init__(self, *args, power, health, damage, inventory, **kwargs):\n", |
521 | | - " super().__init__(*args, **kwargs)\n", |
522 | | - " self.power = power\n", |
523 | | - " self.health = health\n", |
524 | | - " self.damage = damage\n", |
525 | | - " self.inventory = inventory\n", |
526 | | - "\n", |
527 | | - " def introduction(self):\n", |
528 | | - " super().introduction()\n", |
529 | | - " print(f\"파워: {self.power}\")\n", |
530 | | - " print(f\"무기: {self.inventory['weapon']}\")\n", |
531 | | - "\n", |
532 | | - " def attack(self, other):\n", |
533 | | - " print(f\"{self.name}: {other.name} 공격!\")\n", |
534 | | - " attack_power = self.damage * 0.1\n", |
535 | | - " other.health -= attack_power" |
536 | | - ] |
537 | | - }, |
538 | | - { |
539 | | - "cell_type": "code", |
540 | | - "execution_count": 26, |
541 | | - "id": "57ab2808", |
542 | | - "metadata": {}, |
543 | | - "outputs": [ |
544 | | - { |
545 | | - "name": "stdout", |
546 | | - "output_type": "stream", |
547 | | - "text": [ |
548 | | - "이름: 아이언맨\n", |
549 | | - "나이: 45\n", |
550 | | - "성별: 남성\n", |
551 | | - "파워: 100\n", |
552 | | - "무기: 레이저\n" |
553 | | - ] |
554 | | - } |
555 | | - ], |
556 | | - "source": [ |
557 | | - "ironman = Hero(\n", |
558 | | - " \"아이언맨\",\n", |
559 | | - " 45,\n", |
560 | | - " \"남성\",\n", |
561 | | - " power=100,\n", |
562 | | - " health=100,\n", |
563 | | - " damage=200,\n", |
564 | | - " inventory={\"suit\": 500, \"weapon\": \"레이저\"},\n", |
565 | | - ")\n", |
566 | | - "\n", |
567 | | - "ironman.introduction()" |
568 | | - ] |
569 | | - }, |
570 | | - { |
571 | | - "cell_type": "markdown", |
572 | | - "id": "610b2a13", |
573 | | - "metadata": {}, |
574 | | - "source": [ |
575 | | - "### 사용자 정의 메서드 재정의" |
576 | | - ] |
577 | | - }, |
578 | | - { |
579 | | - "cell_type": "markdown", |
580 | | - "id": "06538759", |
581 | | - "metadata": {}, |
582 | | - "source": [ |
583 | | - "부모 클래스에서 선언된 메서드뿐만 아니라,\n", |
584 | | - "자식 클래스에서 이미 정의한 메서드도 필요에 따라 다시 정의할 수 있다.\n", |
585 | | - "\n", |
586 | | - "예를 들어 영웅의 파워가 300 이상이면 공격 효과가 두 배가 되도록 `attack()` 메서드를 다시 정의하자." |
587 | | - ] |
588 | | - }, |
589 | | - { |
590 | | - "cell_type": "code", |
591 | | - "execution_count": 27, |
592 | | - "id": "e6917fd2", |
593 | | - "metadata": {}, |
594 | | - "outputs": [], |
595 | | - "source": [ |
596 | | - "class Hero(Person):\n", |
597 | | - " def __init__(self, *args, power, health, damage, inventory, **kwargs):\n", |
598 | | - " super().__init__(*args, **kwargs)\n", |
599 | | - " self.power = power\n", |
600 | | - " self.health = health\n", |
601 | | - " self.damage = damage\n", |
602 | | - " self.inventory = inventory\n", |
603 | | - "\n", |
604 | | - " def introduction(self):\n", |
605 | | - " super().introduction()\n", |
606 | | - " print(f\"파워: {self.power}\")\n", |
607 | | - " print(f\"무기: {self.inventory['weapon']}\")\n", |
608 | | - "\n", |
609 | | - " def attack(self, other):\n", |
610 | | - " print(f\"{self.name}: {other.name} 공격!\")\n", |
611 | | - " if self.power >= 300:\n", |
612 | | - " attack_power = self.damage * 0.2\n", |
613 | | - " else:\n", |
614 | | - " attack_power = self.damage * 0.1\n", |
615 | | - " other.health -= attack_power" |
616 | | - ] |
617 | | - }, |
618 | | - { |
619 | | - "cell_type": "code", |
620 | | - "execution_count": 28, |
621 | | - "id": "e028d135", |
622 | | - "metadata": {}, |
623 | | - "outputs": [ |
624 | | - { |
625 | | - "name": "stdout", |
626 | | - "output_type": "stream", |
627 | | - "text": [ |
628 | | - "아이언맨 체력: 100\n", |
629 | | - "헐크 체력: 400\n", |
630 | | - "아이언맨: 헐크 공격!\n", |
631 | | - "헐크 체력: 380.0\n", |
632 | | - "헐크: 아이언맨 공격!\n", |
633 | | - "아이언맨 체력: 40.0\n" |
634 | | - ] |
635 | | - } |
636 | | - ], |
637 | | - "source": [ |
638 | | - "ironman = Hero(\"아이언맨\", 45, \"남성\", power=100, health=100, damage=200, inventory={\"suit\": 500, \"weapon\": \"레이저\"})\n", |
639 | | - "hulk = Hero(\"헐크\", 42, \"남성\", power=400, health=400, damage=300, inventory={\"suit\": 0, \"weapon\": \"주먹\"})\n", |
640 | | - "\n", |
641 | | - "print(\"아이언맨 체력:\", ironman.health)\n", |
642 | | - "print(\"헐크 체력:\", hulk.health)\n", |
643 | | - "\n", |
644 | | - "ironman.attack(hulk)\n", |
645 | | - "print(\"헐크 체력:\", hulk.health)\n", |
646 | | - "\n", |
647 | | - "hulk.attack(ironman)\n", |
648 | | - "print(\"아이언맨 체력:\", ironman.health)" |
649 | | - ] |
650 | | - }, |
651 | 519 | { |
652 | 520 | "cell_type": "markdown", |
653 | 521 | "id": "46c1f8b4", |
|
726 | 594 | }, |
727 | 595 | { |
728 | 596 | "cell_type": "code", |
729 | | - "execution_count": 29, |
| 597 | + "execution_count": 13, |
730 | 598 | "id": "65eef2aa", |
731 | 599 | "metadata": {}, |
732 | 600 | "outputs": [], |
|
751 | 619 | }, |
752 | 620 | { |
753 | 621 | "cell_type": "code", |
754 | | - "execution_count": 30, |
| 622 | + "execution_count": 14, |
755 | 623 | "id": "2e00cce3", |
756 | 624 | "metadata": {}, |
757 | 625 | "outputs": [ |
|
797 | 665 | }, |
798 | 666 | { |
799 | 667 | "cell_type": "code", |
800 | | - "execution_count": 31, |
| 668 | + "execution_count": 15, |
801 | 669 | "id": "3ba5f59a", |
802 | 670 | "metadata": {}, |
803 | 671 | "outputs": [], |
|
817 | 685 | }, |
818 | 686 | { |
819 | 687 | "cell_type": "code", |
820 | | - "execution_count": 32, |
| 688 | + "execution_count": 16, |
821 | 689 | "id": "813e1de3", |
822 | 690 | "metadata": {}, |
823 | 691 | "outputs": [], |
|
837 | 705 | }, |
838 | 706 | { |
839 | 707 | "cell_type": "code", |
840 | | - "execution_count": 33, |
| 708 | + "execution_count": 17, |
841 | 709 | "id": "1980deba", |
842 | 710 | "metadata": {}, |
843 | 711 | "outputs": [], |
|
872 | 740 | }, |
873 | 741 | { |
874 | 742 | "cell_type": "code", |
875 | | - "execution_count": 35, |
| 743 | + "execution_count": 18, |
876 | 744 | "id": "5a926361", |
877 | 745 | "metadata": {}, |
878 | 746 | "outputs": [], |
|
911 | 779 | }, |
912 | 780 | { |
913 | 781 | "cell_type": "code", |
914 | | - "execution_count": 36, |
| 782 | + "execution_count": 19, |
915 | 783 | "id": "766a5c2c", |
916 | 784 | "metadata": {}, |
917 | 785 | "outputs": [ |
|
0 commit comments