一、Python中的面向对象编程
everything in python is an object and has a type.
- can create new objects of some type
- can manipulate objects
- can destroy objects
- explicitly using del or just "forget" about them
- python system will reclaim destroyed or inaccessible objects - called "garbage collection"
面向对象编程有很多优势此处不再细说。在Python中使用class关键字定义新类,标准定义如下所示:
# Python
class Coordinate (object):
# define attributes here
圆括号中的object代表Coordinate是Python中的一个对象,并且继承object的所有attributes(即所有的data和procedures)。
- Coordinate is a subclass of object
- object is a superclass of Coordinate
首先,如何定义初始化一个类的方法(method)呢?这时候得使用一个特殊的方法叫做__init__,代码如下所示:
# Python
class Coordinate(object):
""" A coordinate made up of an x and y value """
def _