Wednesday, January 22, 2014

Object-oriented programming

This semester is my first time to get in touch with python. Compared with c++, I think that python is more programmer-friendly.

Since the theme of this paragraph is object-oriented programming , so I plan to talk about some of my peronal with combination of things that I learnt in the class.

First of all,  I would like to introduce the concept of "class".
Generally speaking, the class defines the properties of a things and its behavior.
For example, let's define a new class "dog" .This class will contain some properties about a dog, such as its name, breed and colour

class dog:
          def __init__(self:'dog', breed: str, name: str, colour: str ):
                self.breed=breed
                self.name=name
                self.colour=colour

Note that "__init__" contains the basic information( initial condition) about your class. And what you put in the "__init__" can be used in your following functions.


The second one is object. If we say that "class" is to define the abstract charactertistics of a thing, "object" is an insatnce of this thing. In other words, we can create things in the class we have just defined.For example:

Dog1=dog("Jack","dalmatian",“black and white”)

Then we have a dalmatian dog whose name is Jack and colour is black and white.

Next is about method. "Method",  which can be seen as ability, is used to define what a class can do but may not necessarily to do. For example, just like in the real world, a dog may bark, play, eat,sleep ,etc.
Then we can just define bark,play,eat and sleep as four method under the class dog.
Then after we create a new object , we can use these mathods.
Dog1.bark()
Dog1.play()
Dog1.eat()
Dog1.sleep()

I also met some difficulties when learning object-oriented programming. At first, I always confused class with function. I didn't understand the meaning of a class. I thought every class can be replaced by a combination of funtions!

But after I learned the python example of "stack", I figured out that though some of the classes can be replaced with function, class can make our programming easier, because in object-oriented programming each object sis able to accept data, process the data and communicate with other objects, so they can be seen as small "machines" .















4 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. At first, I also didn't think 'class' makes sense. But when I worked on a program in need of a large bunch of functions, I found the usefulness of class.

    ReplyDelete
  3. Same here, but there was a certain point that I got class is like a factory, it produces a certain type of object. i guess one class represents a certain type, they have the common attribute, common method. Objects of this type has common characteristics that's why they were put in the same class.

    ReplyDelete
  4. Inheritance is a decent property of class.

    ReplyDelete