写在CLASS 前面:python
面向对象术语:类(class)——建立新类app
对象(object)——python中class的新格式,此object也是类,建立的新类即继承了这个类函数
实例(instance)——实例和类就像泥鳅和鱼的关系,泥鳅有鱼的特性,即实例继承类的特性;泥鳅又有本身和其余鱼类不一样的特色,即实例自身的独特性spa
def——class中定义函数code
self——指代被访问的对象或者实例的一个变量?对象
继承(inheritance)——一个类继承另外一个类的特性blog
组合(composition)——一个类将别的类做为部件(车和轮)继承
是什么(is-a)ci
有什么(has-a)get
class的一些表达方式:
class X(Y)——make class named X that is-a Y
class X(object):
def __init__(self, j):
——class X has-a __init__ that takes self and j parameters
class X (object):
def M(self, j):
——class X has-a function named M that takes self and j parameters
foo = X() ——set foo to an instance of class X
foo.M(J) ——from foo get the function M and call it with self and j parameters
foo.K = Q ——from foo get K contribution and set it to Q
————————————————————————————————————————————————————————————————————————————————————
1. class 的 格式:
class X(object):
def __init__(self, y):
def function_a(self, z):
class Animal(object): def __init__(self, name): self.name = name self.specialties = [ ] def voice(self, voice): self.voice = voice print voice def specialty(self, sp): self.specialties.append(sp) d = Animal('dog') d.voice('woooo~') d.specialty('run') print d.specialties c = Animal('cat') c.specialty('climb') print c.specialties
2. 多重继承