-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_inheritance.py
More file actions
66 lines (46 loc) · 1.33 KB
/
class_inheritance.py
File metadata and controls
66 lines (46 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class Dog(object):
# constructor , always happens when creating a class
def __init__(self, name, age): # takes parameters
self.name = name
self.age = age
print("Nice you made a dog object!")
def speak(self):
print('Hi I am', self.name, 'and I am', self.age, 'years old.')
def talk(self):
print('Bark!')
class Cat(Dog):
def __init__(self, name, age, color):
super().__init__(name, age)
self.color = color
def talk(self):
print('Meow!') # overwrites the parent talk method
jim = Dog('jim', 70)
jim.speak()
jim.talk()
tim = Cat('tim', 5, 'black')
tim.speak()
tim.talk()
# use of general classes
class Vehicle(object):
def __init__(self, price, gas, color):
self.price = price
self.gas = gas
self.color = color
def fillUpTank(self):
self.gas = 100
def emptyTank(self):
self.gas = 0
def gasLeft(self):
return self.gas
class Car(Vehicle):
def __init__(self, price, gas, color, speed):
super().__init__(price, gas, color)
self.speed = speed
def beep(self):
print('Beep beep')
class Truck(Vehicle):
def __init__(self, price, gas, color, tires):
super().__init__(price, gas, color)
self.tires = tires
def beep(self):
print('Honk honk')