Monday, February 16, 2015

Week 6: Object-Oriented Programming

I believe that Object-Oriented Programming is a very powerful and extremely useful feature for higher level programming languages. Personally, I love the features that Object-Oriented Programming provides. I remember when I had to create a simple zombie shooter game using only functions.

I'm going to try to give a summary of Object-Oriented Programming in the style of a guide geared towards people who wants to learn Object-Oriented Programming.

I like to compare Object-Oriented Programming to blueprints and the item that can be built from these blueprints. In python terms when we program a class, it's kind of like creating blueprints for an object. Then, with that class (blueprints), we can create many instances of that class.

It's easier to understand with an example. In python this is how we create a class for a person.

class Human:
 
    def __init__(self, eye, skin, height, weight, hair):
        .code here.......
        .code here.......
        .code here.......

Basically, the code above is kind of like the blueprints for creating a human, and whenever we create a human object we can initialize it with his/her eye color, skin color, height, weight, and hair color.

In Python, there are many special procedures that we can define within classes. Some of the ones that I'm familiar with are __eq__, __str__, __repr__, and __init__.

The  __init__ method is required for all classes and basically it is used to initialize an instance of the class. The __str__ method is used to print out what the class does in a human readable form. The __repr__ method prints out a representation of the class with its values. Finally, the __eq__ method allows you to compare two objects and whether or not these two objects are equal.

Another feature of Object-Oriented Programming is inheritance. We can make a class and then inherit and use all of the methods from another class. Using the example above I can make something like...

class HumanChild(Human):
    ....code here

This HumanChild will then be able to use everything from the Human class and we can implement more procedures that would only apply to a child class rather than the general Human class.

We could go more into depth about each section of the code, but this is just a quick general overview of Object-Oriented Programming.


(**Note: On the website it says to write on OOP for week #6, but on handout it says to write it for week #7. I don't think it really matters, so I'm writing on OOP for week #6)

No comments:

Post a Comment