.超继承 当子类需要用到父类的方法,同时也兼并子类的方法时 使用超继承
super(子类名,self).父类方法名与子类相同的方法名(父类的参数)
2.几种方法
构造方法 =实例方法 创建实例化的时候必须传入参数
class person(object):
def init(self,name):
self.name = name
p1 = person(‘jon’)
2,类方法
用@classmethod 装饰的方法是类方法,用类和实例都能访问到,传入的参数第一个是类,不是实例。所以访问不到实例的属性。
#encoding:utf-8
#小五
class Person(object):
gender=‘女’
def init(self,name):
self.name = name
@classmethod
def sayD(self):
print self.gender #可以正常输出‘女’
return self.name #报错:AttributeError: type object ‘Person’ has no attribute ‘name’
def sayHello(self):
return self.name+’! hello!’
def str(self):
return ‘Person name(%s)’%self.name
p1 = Person(‘Bob’)
print p1.sayD()
3,静态方法
用@staticmethod装饰的方法称为静态方法,和前2种