一、Python中的类和继承
之前提到的Coordinate (object)中的object代表了Python中的一个类,但其实这里指代的是一个超类(superclass)或者说父类(parent class)。
# Python
class Coordinate (object)
object只是Python中最基础的类而已,你可以指定不同的父类。
# Python
class Animal (object) :
def __init__(self, age) :
self.age = age
self.name = None
# name is a data attribute even though an instance is not
# initialized with it as a param
myanimal = Animal(3)
在定义初始化函数的时候,没有必要给定所有的形参,例如上面代码中的name。
在定义类时,尽量都定义getter和setter方法,如下所示:
# Python
class Animal(object):
def __init__(self, age):
self.age = age
self.name = None
def get_age(self):
return self.age
def get_name(self):
return self.name
def set_age(self, newage):
self.age = newage
def set_name(self, newname="&