2015年2月17日星期二

Summary of Object-Oriented Programming

In the last class of CSC108, we began learning Object-Oriented Programming. The instructor introduced us of class and object. At that time, I was totally confused by Object-Oriented Programming and Procedure-Oriented Programming and cannot tell the difference between them. But after learning CSC148 for a couple of weeks, I realize how different they are.

In first week's lesson, Danny used a turtle example to show us what is OOP. To me, it seems that everything in Python is an object, a function is an object, a list is an object and even a class is an object. And unlike Procedure-Oriented Programming language such as C, python define a function instead of declare the output type, so basically it is creating a function instead of defining a procedure.

Then, we learnt some methods of object such as __eq__, __str__ and __repr__. At the first time, I don't know when to use __str__ and when to use __repr__. Danny showed us an example of Point. The only difference between the outputs of str and repr is the class name 'Point', which is for python to evaluate. Therefore, __repr__ is an official representation of the object and can be evaluated by python to give back the object while the __str__ method is used to produce a human-readable string of that particular object. __eq__ is simpler compare to the two methods before, it is more like giving the programmer a way to define what properties do two objects need to be called 'equal'.

Afterwards, we learnt the property methods, which is used by programmer to prevent users from modifying important data. It is somehow more interesting to me than the concepts I've learnt before because it seems that only Python has such a method for programmers to protect the data.

没有评论:

发表评论