Skip to content

Commit f17171c

Browse files
committed
Update oop-inheritance.ipynb
1 parent fa28b29 commit f17171c

1 file changed

Lines changed: 51 additions & 183 deletions

File tree

oop-inheritance.ipynb

Lines changed: 51 additions & 183 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
},
6363
{
6464
"cell_type": "code",
65-
"execution_count": 2,
65+
"execution_count": 1,
6666
"id": "8a3b104b",
6767
"metadata": {},
6868
"outputs": [],
@@ -89,7 +89,7 @@
8989
},
9090
{
9191
"cell_type": "code",
92-
"execution_count": 3,
92+
"execution_count": 2,
9393
"id": "70a3e601",
9494
"metadata": {},
9595
"outputs": [],
@@ -108,7 +108,7 @@
108108
},
109109
{
110110
"cell_type": "code",
111-
"execution_count": 4,
111+
"execution_count": 3,
112112
"id": "4355b15b",
113113
"metadata": {},
114114
"outputs": [
@@ -155,7 +155,7 @@
155155
},
156156
{
157157
"cell_type": "code",
158-
"execution_count": 16,
158+
"execution_count": 4,
159159
"id": "117bef29",
160160
"metadata": {},
161161
"outputs": [],
@@ -191,7 +191,7 @@
191191
},
192192
{
193193
"cell_type": "code",
194-
"execution_count": 17,
194+
"execution_count": 5,
195195
"id": "53a5fb9f",
196196
"metadata": {},
197197
"outputs": [
@@ -284,7 +284,7 @@
284284
},
285285
{
286286
"cell_type": "code",
287-
"execution_count": 18,
287+
"execution_count": 6,
288288
"id": "65a01513",
289289
"metadata": {},
290290
"outputs": [],
@@ -314,7 +314,7 @@
314314
},
315315
{
316316
"cell_type": "code",
317-
"execution_count": 19,
317+
"execution_count": 7,
318318
"id": "6dcd2d8d",
319319
"metadata": {},
320320
"outputs": [
@@ -324,7 +324,7 @@
324324
"(True, True)"
325325
]
326326
},
327-
"execution_count": 19,
327+
"execution_count": 7,
328328
"metadata": {},
329329
"output_type": "execute_result"
330330
}
@@ -345,7 +345,7 @@
345345
},
346346
{
347347
"cell_type": "code",
348-
"execution_count": 20,
348+
"execution_count": 8,
349349
"id": "940fdf90",
350350
"metadata": {},
351351
"outputs": [
@@ -374,7 +374,7 @@
374374
},
375375
{
376376
"cell_type": "code",
377-
"execution_count": 21,
377+
"execution_count": 9,
378378
"id": "ceeaf5d4",
379379
"metadata": {},
380380
"outputs": [
@@ -395,6 +395,33 @@
395395
" print(\"일반 사람은 attack() 메서드를 갖고 있지 않습니다.\")"
396396
]
397397
},
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+
},
398425
{
399426
"cell_type": "markdown",
400427
"id": "a82e686d",
@@ -425,16 +452,18 @@
425452
"그런 다음 `Hero`의 `__init__()` 메서드는 영웅에게만 필요한\n",
426453
"`power`, `health`, `damage`, `inventory` 속성을 추가로 초기화한다.\n",
427454
"\n",
428-
"이처럼 부모 클래스에서 상속받은 메서드를 자식 클래스에서 같은 이름으로 다시 정의하는 것을\n",
455+
"**메서드 오버라이딩**\n",
456+
"\n",
457+
"부모 클래스에서 상속받은 메서드를 자식 클래스에서 같은 이름으로 다시 정의하는 것을\n",
429458
"**메서드 오버라이딩**<font size='2'>method overriding</font>이라고 한다.\n",
430459
"\n",
431-
"예를 들어 `Hero`의 `introduction()` 메서드를 재정의하여\n",
432-
"일반적인 자기소개에 파워와 무기 정보를 추가할 수 있다."
460+
"아래 `Hero` 클래스는 부모 클래스인 `Person` 클래스로부터 상속받은 `introduction()` 메서드도 재정의하여\n",
461+
"일반적인 자기소개와 함께 파워와 무기 정보도 제공한다."
433462
]
434463
},
435464
{
436465
"cell_type": "code",
437-
"execution_count": 22,
466+
"execution_count": 11,
438467
"id": "9e0e9310",
439468
"metadata": {},
440469
"outputs": [],
@@ -460,7 +489,7 @@
460489
},
461490
{
462491
"cell_type": "code",
463-
"execution_count": 23,
492+
"execution_count": 12,
464493
"id": "22a3cf8a",
465494
"metadata": {},
466495
"outputs": [
@@ -487,167 +516,6 @@
487516
"print(\"헐크 체력:\", hulk.health)"
488517
]
489518
},
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-
},
651519
{
652520
"cell_type": "markdown",
653521
"id": "46c1f8b4",
@@ -726,7 +594,7 @@
726594
},
727595
{
728596
"cell_type": "code",
729-
"execution_count": 29,
597+
"execution_count": 13,
730598
"id": "65eef2aa",
731599
"metadata": {},
732600
"outputs": [],
@@ -751,7 +619,7 @@
751619
},
752620
{
753621
"cell_type": "code",
754-
"execution_count": 30,
622+
"execution_count": 14,
755623
"id": "2e00cce3",
756624
"metadata": {},
757625
"outputs": [
@@ -797,7 +665,7 @@
797665
},
798666
{
799667
"cell_type": "code",
800-
"execution_count": 31,
668+
"execution_count": 15,
801669
"id": "3ba5f59a",
802670
"metadata": {},
803671
"outputs": [],
@@ -817,7 +685,7 @@
817685
},
818686
{
819687
"cell_type": "code",
820-
"execution_count": 32,
688+
"execution_count": 16,
821689
"id": "813e1de3",
822690
"metadata": {},
823691
"outputs": [],
@@ -837,7 +705,7 @@
837705
},
838706
{
839707
"cell_type": "code",
840-
"execution_count": 33,
708+
"execution_count": 17,
841709
"id": "1980deba",
842710
"metadata": {},
843711
"outputs": [],
@@ -872,7 +740,7 @@
872740
},
873741
{
874742
"cell_type": "code",
875-
"execution_count": 35,
743+
"execution_count": 18,
876744
"id": "5a926361",
877745
"metadata": {},
878746
"outputs": [],
@@ -911,7 +779,7 @@
911779
},
912780
{
913781
"cell_type": "code",
914-
"execution_count": 36,
782+
"execution_count": 19,
915783
"id": "766a5c2c",
916784
"metadata": {},
917785
"outputs": [

0 commit comments

Comments
 (0)