Saturday, February 21, 2015

Summary of "OOP"

    Hey everyone! Hope you all are getting the most out of your reading week. This week's blog is supposed to be on the topic; "summary of Object-Oriented Programming concepts".
First before going for the topic I would like to say that if you have not started on the assignment yet, you really should soon! I started working on the paper and just play around with some Tippy Games! It will give you a great idea. Also the more you read the section on Minimax in the handout, the better you'll be able to start working on it! If you think about it, I think Minimax is the coolest thing that we can do by knowing the concept of recursion up until now

Back to out topic I would like to go through some of the definitions and things we came across in OOP(Object Oriented Programming)



What Is an Object?

An object is a software bundle of related state and behavior. Software objects are often used to model the real-world objects that you find in everyday life. This lesson explains how state and behavior are represented within an object, introduces the concept of data encapsulation, and explains the benefits of designing your software in this manner.

What Is a Class?

A class is a blueprint or prototype from which objects are created. This section defines a class that models the state and behavior of a real-world object. It intentionally focuses on the basics, showing how even a simple class can cleanly model state and behavior.

What Is Inheritance?

Inheritance provides a powerful and natural mechanism for organizing and structuring your software. This section explains how classes inherit state and behavior from their super-classes, and explains how to derive one class from another using the simple syntax provided by the python programming language.

Creating instance objects:

To create instances of a class, you call the class using class name and pass in whatever arguments its__init__ method accepts.

Accessing attributes:

You access the object's attributes using the dot operator with object. Class variable would be accessed using class name as follows

Class Inheritance:

Instead of starting from scratch, you can create a class by deriving it from a preexisting class by listing the parent class in parentheses after the new class name.
The child class inherits the attributes of its parent class, and you can use those attributes as if they were defined in the child class. A child class can also override data members and methods from the parent.

#!/usr/bin/python

class Parent:        # define parent class
   parentAttr = 100
   def __init__(self):
      print "Calling parent constructor"

   def parentMethod(self):
      print 'Calling parent method'

   def setAttr(self, attr):
      Parent.parentAttr = attr

   def getAttr(self):
      print "Parent attribute :", Parent.parentAttr

class Child(Parent): # define child class
   def __init__(self):
      print "Calling child constructor"

   def childMethod(self):
      print 'Calling child method'

c = Child()          # instance of child
c.childMethod()      # child calls its method
c.parentMethod()     # calls parent's method
c.setAttr(200)       # again call parent's method
c.getAttr()          # again call parent's method

No comments:

Post a Comment