How coincidental is that I am writing on the first day of reading week and the evening of Valentine's Day?
#Sad life of a (U of T)student...
Since it's such a special day, let me have some fun before I start rambling about objected oriented programming!
After surviving the past few weeks, I just feel so obliged to share this with you:
Summary of OOP Concepts
A world of classes, objects(data types), interface, and abstract data types.
To be honest, I am not the best at summarizing things...I prefer to write essays instead.
:) But I will try my best here.
OOP, also known as objected oriented programming, is a programming paradigm based on objects(bundles of bits) and data rather than logic.
Back in 108, we mainly exercised in procedural programming, where we focus on writing functions(procedures on data).
OOP opened the gates to a vast new world, one that is full of complexity and data. The essence of OOP lies in higher level thinking about the organization and structure of data. The creation of classes allow the creation of many objects and a useful interface; we can thus create distinct entities that have their own functionality and data.
In class, we have mostly emphasized on class design and inheritance.
Class design can be a nebulous concept because there are so many ways to design a class! You can choose to only have parameter self in the constructor and have data storage internalized, and have methods for manipulation; on the other hand, you can choose to have different parameters for user input.
But I believe it is always useful to ask yourself a couple of questions before you write a class:
1. What kind of data are you processing?
2. What kind of objects would you want? Look for key words!
3. What attributes do you want for your objects? For example, a turtle class would want to have a tail as its attribute.
Inheritance is what it suggest; it is the idea of having a child class "inheriting" attributes from the parent class.
To illustrate its connotation in the English language, check out the classes I have here:
class Coco:
''' Coco is the parent of Joan'''
def __init__(self):
self.hair_colour = 'blue'
self.eye = 'black'
self.wings = 'green'
class Joan(Coco):
''' Joan is the child of Coco'''
def __init__(self):
Coco.__init__(self)
self.wings = 'orange' # I am a defiant DNA.
Basically, Joan inherits all attributes of Coco(blue hair, black eyes) but may not be identical (have orange wings) as Coco.
To further illustrate the utility of OOP in general and summarize it, let us take a look at another real world example.
Mr.Potter recently opened a restaurant on Bay Street; he then wonders how he should process orders of his numerous customers without abusing his employees' memory capacity.
So Mr.Potter can construct a class named OrderSystem and a class Order. OrderSystem can have attributes such as active_orders, implemented_orders, total_customers, and various methods. Class Order can have more specific attributes such as customer_name, table_number, time_of_order, and total_price. With each new order, a new object Order is created and stores the information of that specific table.
i.e.
class OrderSystem:
''' A data system that stores information of orders.'''
def __init__(self):
self.active_orders = []
self.implemented_orders = []
total_customers = 0
def add_customer(self, name):
pass
class Order(OrderSystem):
''' Class for specific order, inherits from OrderSystem.'''
def __init__(self, customer_name, table_number, time_of_order):
OrderSystem.__init__(self)
self.customer_name = customer_name
self.table = table_number
self.time_of_order = [time_of_order]
self.total_price = 0
def add_dish(self, dish, order_time):
pass
Personally, I think OOP is a very valuable, intriguing concept that is very practical. When solving problems in the real world, we often face a large pool of data(whether it be a list of patients, data tables of weather patterns or bank account data). OOP in turn allow us to store and process this data in a fairly organized manner.
Also, as I was walking around my classmates' blogs, I found this classmate of mine, (http://whydopigsquack.blogspot.ca/) wrote a very nice summary of OOP concepts. Hence do keep in mind that if you do not find mine most helpful, you can always take a look there. :)
You can also consult http://openbookproject.net/thinkcs/python/english3e/index.html , which I draw some of my information from.
---------------------------------------FLYINGFISH's FINISH LINE--------------------------------------------
Thank you for reading! If you are nice, please comment below so I know you are there! :)




No comments:
Post a Comment