|
| 1 | +# A Python Class |
| 2 | +class Superclass(): |
| 3 | + def __init__(self, color, height, width): |
| 4 | + self.height = height |
| 5 | + self.color = color |
| 6 | + self.width = width |
| 7 | + |
| 8 | + def does_stuff(self): |
| 9 | + print('This method does stuff') |
| 10 | + |
| 11 | + |
| 12 | +# Inheriting From A Superclass |
| 13 | +class Subclass(Superclass): |
| 14 | + pass |
| 15 | + |
| 16 | + |
| 17 | +obj1 = Subclass('Red', '5 feet', 100) |
| 18 | + |
| 19 | +# The Child Has Now Inherited Attributes And Methods |
| 20 | +print(type(obj1)) |
| 21 | +# <class '__main__.Subclass'> |
| 22 | + |
| 23 | +print(isinstance(obj1, Subclass)) |
| 24 | +# True |
| 25 | + |
| 26 | +print(obj1.color) |
| 27 | +# Red |
| 28 | + |
| 29 | +print(obj1.height) |
| 30 | +# 5 feet |
| 31 | + |
| 32 | +print(obj1.width) |
| 33 | +# 100 |
| 34 | + |
| 35 | +obj1.does_stuff() |
| 36 | + |
| 37 | + |
| 38 | +# This method does stuff |
| 39 | + |
| 40 | + |
| 41 | +# Classes Without Inheritance |
| 42 | +class Inputdevice: |
| 43 | + def __init__(self, devicetype, inputconnector, bluetooth, manufacturer): |
| 44 | + self.devicetype = devicetype |
| 45 | + self.manufacturer = manufacturer |
| 46 | + self.inputconnector = inputconnector |
| 47 | + self.bluetooth = bluetooth |
| 48 | + |
| 49 | + |
| 50 | +class Outputdevice: |
| 51 | + def __init__(self, devicetype, connector, manufacturer, outrate): |
| 52 | + self.devicetype = devicetype |
| 53 | + self.manufacturer = manufacturer |
| 54 | + self.outrate = outrate |
| 55 | + self.connector = connector |
| 56 | + |
| 57 | + |
| 58 | +class IODevice: |
| 59 | + def __init__(self, devicetype, connector, manufacturer, outrate): |
| 60 | + self.devicetype = devicetype |
| 61 | + self.manufacturer = manufacturer |
| 62 | + self.outrate = outrate |
| 63 | + self.connector = connector |
| 64 | + |
| 65 | + |
| 66 | +input1 = Inputdevice("Keyboard", "usb", True, "Lenovo") |
| 67 | +io1 = IODevice("Flash Drive", "usb", "Sandisk", "35MB ps") |
| 68 | +output1 = Outputdevice("Monitor", "HDMI", "Samsung", "18Gbps") |
| 69 | + |
| 70 | +print("This device has a " + input1.inputconnector + " connector") |
| 71 | +# This device has a usb connector |
| 72 | + |
| 73 | +print(io1.manufacturer + " is the device manufacturer") |
| 74 | +# Sandisk is the device manufacturer |
| 75 | + |
| 76 | +print(input1.manufacturer + " " + input1.devicetype) |
| 77 | +# Lenovo Keyboard |
| 78 | + |
| 79 | +print(output1.manufacturer + " " + output1.devicetype) |
| 80 | +# Samsung Monitor |
| 81 | + |
| 82 | + |
| 83 | +# Rewriting Classes With Inheritance |
| 84 | +class Peripheral: |
| 85 | + def __init__(self, devicetype, manufacturer): |
| 86 | + self.devicetype = devicetype |
| 87 | + self.manufacturer = manufacturer |
| 88 | + |
| 89 | + |
| 90 | +class Outputperipheral(Peripheral): |
| 91 | + def __init__(self, devicetype, manufacturer, connector, outrate): |
| 92 | + super().__init__(devicetype, manufacturer) |
| 93 | + self.outrate = outrate |
| 94 | + self.connector = connector |
| 95 | + |
| 96 | + |
| 97 | +class Inputdevice(Peripheral): |
| 98 | + def __init__(self, devicetype, inputconnector, bluetooth, manufacturer): |
| 99 | + super().__init__(devicetype, manufacturer) |
| 100 | + self.inputconnector = inputconnector |
| 101 | + self.bluetooth = bluetooth |
| 102 | + |
| 103 | + |
| 104 | +class Outputdevice(Outputperipheral): |
| 105 | + def __init__(self, devicetype, connector, manufacturer, outrate): |
| 106 | + super().__init__(devicetype, manufacturer, connector, outrate) |
| 107 | + |
| 108 | + |
| 109 | +class IODevice(Outputperipheral): |
| 110 | + def __init__(self, devicetype, connector, manufacturer, outrate): |
| 111 | + super().__init__(devicetype, manufacturer, connector, outrate) |
| 112 | + |
| 113 | + |
| 114 | +input1 = Inputdevice("Keyboard", "usb", True, "Lenovo") |
| 115 | +io1 = IODevice("Flash Drive", "usb", "Sandisk", "35MB ps") |
| 116 | +output1 = Outputdevice("Monitor", "HDMI", "Samsung", "18Gbps") |
| 117 | + |
| 118 | +print("This device has a " + input1.inputconnector + " connector") |
| 119 | +# This device has a usb connector |
| 120 | + |
| 121 | +print(io1.manufacturer + " is the device manufacturer") |
| 122 | +# Sandisk is the device manufacturer |
| 123 | + |
| 124 | +print(input1.manufacturer + " " + input1.devicetype) |
| 125 | +# Lenovo Keyboard |
| 126 | + |
| 127 | +print(output1.manufacturer + " " + output1.devicetype) |
| 128 | +# Samsung Monitor |
0 commit comments