This repository was archived by the owner on May 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjectOrientedProg.py
More file actions
70 lines (55 loc) · 2.44 KB
/
Copy pathobjectOrientedProg.py
File metadata and controls
70 lines (55 loc) · 2.44 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
67
68
69
70
class Animal:
category = ""
# class-level default attribute
def __init__(self, name):
self.name = name
# allowing each animal to set a unique name at
# creation, this is an instance-level attribute
def set_category(self, category):
self.category = category
# setting instance-level attribute category
# to class-level attribute
class Turtle(Animal):
# child class of parent class Animal, will
# inherit everything from parent class Animal
category = "reptile"
# setting class-level attribute here will overwrite
# parent class Animal class-level category attribute
class Snake(Animal):
category = "reptile"
class Zoo:
def __init__(self):
# creating a dictionary to store the animals at instance-level
self.current_animals = {}
def add_animal(self, animal):
"""Adding animals to a dict with names as keys and category as
values. This will be passed in from outside of the class
Args:
animal (instance of class Animal): class composition magic
"""
self.current_animals[animal.name] = animal.category
# we have full access to attributes and methods via
# composition and able to see the attributes: name, category
def total_of_category(self, category):
# get total number of animals for matched input param,
# input param will be passed in from outside the class
result = 0
for animal in self.current_animals.values():
# only looping through the values of dict and not keys
# which is category to check if matches input param
if animal == category:
result += 1
return result
zoo = Zoo() #create an instance of Zoo class
turtle = Turtle("Turtle") #create an instance of the Turtle class
snake = Snake("Snake") #create an instance of the Snake class
zoo.add_animal(turtle)
# add animal to zoo, pay extra attention here as an object of class Turtle
# is being inserted into the method as an argument, this is the perfect
# example of composition as this will enable the class Zoo to have full
# access to everything the turtle object has access to including all
# attributes and methods
zoo.add_animal(snake)
# again same thing is happening here as a perfect example of composition
print(zoo.total_of_category("reptile"))
# total number of animals in this category