Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8cc900e

Browse files
committed
Basic Inheritance Examples
1 parent 29ca69f commit 8cc900e

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ files, and the full tutorial for each collection of code is available below.
3333
26. [Object Oriented Python](https://vegibit.com/object-oriented-python/)
3434
27. [Instance Methods And Attributes](https://vegibit.com/python-instance-methods-and-attributes/)
3535
28. [How To Use type() And isinstance()](https://vegibit.com/how-to-use-type-and-isinstance-in-python/)
36+
29. [Basic Python Inheritance](https://vegibit.com/basic-python-inheritance/)

inheritance.py

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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

Comments
 (0)