|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 13, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [ |
| 8 | + { |
| 9 | + "name": "stdout", |
| 10 | + "output_type": "stream", |
| 11 | + "text": [ |
| 12 | + "Woof!\n", |
| 13 | + "Piper 5 9.5\n", |
| 14 | + "Aroooo\n", |
| 15 | + "Chloe 5 10\n", |
| 16 | + "Aroooo\n" |
| 17 | + ] |
| 18 | + } |
| 19 | + ], |
| 20 | + "source": [ |
| 21 | + "class Dog:\n", |
| 22 | + " def __init__(self, name, age, friendliness):\n", |
| 23 | + " self.name = name\n", |
| 24 | + " self.age = age\n", |
| 25 | + " self.friendliness = friendliness\n", |
| 26 | + " \n", |
| 27 | + " def likes_walks(self):\n", |
| 28 | + " return True\n", |
| 29 | + " \n", |
| 30 | + " def bark(self):\n", |
| 31 | + " return 'Woof!'\n", |
| 32 | + "\n", |
| 33 | + "# Inheritance - Add Parent Class in Class Definition\n", |
| 34 | + "# super() - Parent Class\n", |
| 35 | + "class Samyoed(Dog):\n", |
| 36 | + " def __init__(self, name, age, friendliness):\n", |
| 37 | + " super().__init__(name, age, friendliness) # Calls initialization function in the parent class\n", |
| 38 | + "\n", |
| 39 | + "\n", |
| 40 | + " def bark(self):\n", |
| 41 | + " return 'Arf Arf!'\n", |
| 42 | + "\n", |
| 43 | + "class Poodle(Dog):\n", |
| 44 | + " def __init__(self, name, age, friendliness):\n", |
| 45 | + " super().__init__(self, name, age, friendliness) # Calls initialization function in the parent class\n", |
| 46 | + "\n", |
| 47 | + "class GoldenRetriever(Dog):\n", |
| 48 | + " def __init__(self, name, age, friendliness):\n", |
| 49 | + " super().__init__(name, age, friendliness) # Calls initialization function in the parent class\n", |
| 50 | + "\n", |
| 51 | + "# poodle = Poodle('Poodini', 5, 10)\n", |
| 52 | + "sammy = Samyoed('Sammy', 2, 10)\n", |
| 53 | + "# print(sammy.name, sammy.age, sammy.friendliness)\n", |
| 54 | + "print(sammy.bark())\n", |
| 55 | + "class BlackLab(Dog):\n", |
| 56 | + " def __init__(self, name, age, friendliness):\n", |
| 57 | + " super().__init__(name, age, friendliness)\n", |
| 58 | + "\n", |
| 59 | + "class BlackRetriever(BlackLab, GoldenRetriever):\n", |
| 60 | + " def __init__(self, name, age, friendliness):\n", |
| 61 | + " super().__init__(name, age,friendliness)\n", |
| 62 | + " def bark(self):\n", |
| 63 | + " return 'Aroooo'\n", |
| 64 | + "\n", |
| 65 | + "# Create New Objects\n", |
| 66 | + "Piper = BlackRetriever('Piper', 5, 9.5)\n", |
| 67 | + "Chloe = BlackRetriever('Chloe', 5, 10)\n", |
| 68 | + "print(Piper.name, Piper.age, Piper.friendliness) \n", |
| 69 | + "print(Piper.bark())\n", |
| 70 | + "print(Chloe.name, Chloe.age, Chloe.friendliness)\n", |
| 71 | + "print(Chloe.bark())\n", |
| 72 | + "\n", |
| 73 | + "\n" |
| 74 | + ] |
| 75 | + }, |
| 76 | + { |
| 77 | + "cell_type": "code", |
| 78 | + "execution_count": null, |
| 79 | + "metadata": {}, |
| 80 | + "outputs": [], |
| 81 | + "source": [] |
| 82 | + } |
| 83 | + ], |
| 84 | + "metadata": { |
| 85 | + "kernelspec": { |
| 86 | + "display_name": "Python 3.9.12 ('macos')", |
| 87 | + "language": "python", |
| 88 | + "name": "python3" |
| 89 | + }, |
| 90 | + "language_info": { |
| 91 | + "codemirror_mode": { |
| 92 | + "name": "ipython", |
| 93 | + "version": 3 |
| 94 | + }, |
| 95 | + "file_extension": ".py", |
| 96 | + "mimetype": "text/x-python", |
| 97 | + "name": "python", |
| 98 | + "nbconvert_exporter": "python", |
| 99 | + "pygments_lexer": "ipython3", |
| 100 | + "version": "3.9.12" |
| 101 | + }, |
| 102 | + "orig_nbformat": 4, |
| 103 | + "vscode": { |
| 104 | + "interpreter": { |
| 105 | + "hash": "22608493959378f606de73a65fea1c2a2865a76055a64344b8428e775cc448e8" |
| 106 | + } |
| 107 | + } |
| 108 | + }, |
| 109 | + "nbformat": 4, |
| 110 | + "nbformat_minor": 2 |
| 111 | +} |
0 commit comments