Saturday, January 25, 2014

Week 3 ——Object-Oriented Programming

   During the first three weeks of course CSC148, I learned ways of Object-Oriented Programming like class, and stack. Object-Oriented Programming (OOP) is a programming paradigm using data fields and methods of “objects”-data structures for the design of applications and computer programs. In the class method, we use __init__(two underlines on both sides) to initialize the program and we can also add the initial values after __init__. The specific method is defined after the term “def”, it is not a function, because for every method, “self” is always the first parameter, but when we use it, we do not need to provide it, because the program will automatically bind the first parameter to the corresponding example. The methods and features of python are acquiescently public or common, and if we want to make it private or for our own, we need to use the double underlines before the name of the method. When writing another class name after the “class-method"sentence, and this class name will inherit the specific class. A good example for class-method is:

class Point:
      """ Point class represents and manipulates x,y coords. """
        
     def __init__(self):
           """ Create a new point at the origin """
           self.x = 0
           self.y = 0
            p = Point()         # Instantiate an object of type Point
            q = Point()         # Make a second point

   print(p.x, p.y, q.x, q.y)  # Each point object has its own x and y
    >>> p.x = 3
    >>> p.y = 4
    >>> print(p.y)
    4
    >>> x = p.x
    >>> print(x)
    3
  As for stack, it is a kind of built-in list of python, and we need to remember the basic methods that are: 
 (1):__init__  Initialize a new empty stack.
   (2):  push  add a new item to the stack.
   (3) : pop Remove and return an item from the stack. 
   (4) : is_empty Check whether the stack is empty.
   
>>> stack = [3,4,5]
>>> stack.append(6)
>>> stack.append(7)
>>> stack
{3,4,5,6,7]
>>> stack.pop()
7
>>>stack
[3,4,5,6]
>>> stack.pop()
6
>>>stack.pop()
5
>>> stack
[3,4]

No comments:

Post a Comment