|
2 | 2 | "cells": [
|
3 | 3 | {
|
4 | 4 | "cell_type": "code",
|
5 |
| - "execution_count": 4, |
| 5 | + "execution_count": 5, |
6 | 6 | "metadata": {},
|
7 | 7 | "outputs": [
|
8 | 8 | {
|
|
38 | 38 | "print(\"{} is {} years old\".format( blu.name, blu.age))\n",
|
39 | 39 | "print(\"{} is {} years old\".format( woo.name, woo.age))"
|
40 | 40 | ]
|
| 41 | + }, |
| 42 | + { |
| 43 | + "cell_type": "code", |
| 44 | + "execution_count": 7, |
| 45 | + "metadata": {}, |
| 46 | + "outputs": [ |
| 47 | + { |
| 48 | + "name": "stdout", |
| 49 | + "output_type": "stream", |
| 50 | + "text": [ |
| 51 | + "Bird is ready\n", |
| 52 | + "Penguin is ready\n", |
| 53 | + "Penguin\n", |
| 54 | + "Swim faster\n", |
| 55 | + "Run faster\n" |
| 56 | + ] |
| 57 | + } |
| 58 | + ], |
| 59 | + "source": [ |
| 60 | + "# parent class\n", |
| 61 | + "class Bird:\n", |
| 62 | + " \n", |
| 63 | + " def __init__(self):\n", |
| 64 | + " print(\"Bird is ready\")\n", |
| 65 | + "\n", |
| 66 | + " def whoisThis(self):\n", |
| 67 | + " print(\"Bird\")\n", |
| 68 | + "\n", |
| 69 | + " def swim(self):\n", |
| 70 | + " print(\"Swim faster\")\n", |
| 71 | + "\n", |
| 72 | + "# child class\n", |
| 73 | + "class Penguin(Bird):\n", |
| 74 | + "\n", |
| 75 | + " def __init__(self):\n", |
| 76 | + " # call super() function\n", |
| 77 | + " super().__init__()\n", |
| 78 | + " print(\"Penguin is ready\")\n", |
| 79 | + "\n", |
| 80 | + " def whoisThis(self):\n", |
| 81 | + " print(\"Penguin\")\n", |
| 82 | + "\n", |
| 83 | + " def run(self):\n", |
| 84 | + " print(\"Run faster\")\n", |
| 85 | + "\n", |
| 86 | + "peggy = Penguin()\n", |
| 87 | + "peggy.whoisThis()\n", |
| 88 | + "peggy.swim()\n", |
| 89 | + "peggy.run()" |
| 90 | + ] |
| 91 | + }, |
| 92 | + { |
| 93 | + "cell_type": "code", |
| 94 | + "execution_count": 10, |
| 95 | + "metadata": {}, |
| 96 | + "outputs": [ |
| 97 | + { |
| 98 | + "name": "stdout", |
| 99 | + "output_type": "stream", |
| 100 | + "text": [ |
| 101 | + "Selling Price: 900\n", |
| 102 | + "Selling Price: 900\n", |
| 103 | + "Selling Price: 1000\n" |
| 104 | + ] |
| 105 | + } |
| 106 | + ], |
| 107 | + "source": [ |
| 108 | + "### Encapsulation\n", |
| 109 | + "class Computer:\n", |
| 110 | + "\n", |
| 111 | + " def __init__(self):\n", |
| 112 | + " self.__maxprice = 900\n", |
| 113 | + "\n", |
| 114 | + " def sell(self):\n", |
| 115 | + " print(\"Selling Price: {}\".format(self.__maxprice))\n", |
| 116 | + "\n", |
| 117 | + " def setMaxPrice(self, price):\n", |
| 118 | + " self.__maxprice = price\n", |
| 119 | + "\n", |
| 120 | + "c = Computer()\n", |
| 121 | + "c.sell()\n", |
| 122 | + "\n", |
| 123 | + "# change the price\n", |
| 124 | + "c.__maxprice = 1000\n", |
| 125 | + "c.sell()\n", |
| 126 | + "\n", |
| 127 | + "# using setter function\n", |
| 128 | + "c.setMaxPrice(1000)\n", |
| 129 | + "c.sell()" |
| 130 | + ] |
| 131 | + }, |
| 132 | + { |
| 133 | + "cell_type": "code", |
| 134 | + "execution_count": null, |
| 135 | + "metadata": {}, |
| 136 | + "outputs": [], |
| 137 | + "source": [ |
| 138 | + "### Polymorphism \n", |
| 139 | + "class Parrot:\n", |
| 140 | + "\n", |
| 141 | + " def fly(self):\n", |
| 142 | + " print(\"Parrot can fly\")\n", |
| 143 | + " \n", |
| 144 | + " def swim(self):\n", |
| 145 | + " print(\"Parrot can't swim\")\n", |
| 146 | + "\n", |
| 147 | + "class Penguin:\n", |
| 148 | + "\n", |
| 149 | + " def fly(self):\n", |
| 150 | + " print(\"Penguin can't fly\")\n", |
| 151 | + " \n", |
| 152 | + " def swim(self):\n", |
| 153 | + " print(\"Penguin can swim\")\n", |
| 154 | + "\n", |
| 155 | + "# common interface\n", |
| 156 | + "def flying_test(bird):\n", |
| 157 | + " bird.fly()\n", |
| 158 | + "\n", |
| 159 | + "#instantiate objects\n", |
| 160 | + "blu = Parrot()\n", |
| 161 | + "peggy = Penguin()\n", |
| 162 | + "\n", |
| 163 | + "# passing the object\n", |
| 164 | + "flying_test(blu)\n", |
| 165 | + "flying_test(peggy)" |
| 166 | + ] |
41 | 167 | }
|
42 | 168 | ],
|
43 | 169 | "metadata": {
|
|
0 commit comments