forked from SoftwareLiteracyFoundation/Python-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram_11_Classes.py
More file actions
39 lines (30 loc) · 852 Bytes
/
program_11_Classes.py
File metadata and controls
39 lines (30 loc) · 852 Bytes
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
# define a class called Animal that can make a sound
class Animal:
def __init__(self):
self.name = None
self.sound = None
def PrintSound(self):
print( "My sound is ", self.sound )
def PrintName(self):
print( "I am a ", self.name )
# Derive a Dog from the Animal class
# Animal is the parent class, Dog is the child class
class Dog(Animal):
def __init__(self):
Animal.name = 'Dog'
Animal.sound = 'bark'
# Derive a Cat from the Animal class
class Cat(Animal):
def __init__(self):
self.name = 'Cat'
self.sound = 'meow'
def main():
# See what the animals do
animal_1 = Dog()
animal_1.PrintName()
animal_1.PrintSound()
animal_2 = Cat()
animal_2.PrintName()
animal_2.PrintSound()
if __name__ == "__main__":
main()