标签:
python类的继承关系
class studeent(object): 
    “”“docstring for studeent”“” 
    def init(self, arg): 
        super(studeent, self).init() 
        self.arg = arg 
s1=studeent(23) 
print s1.arg 
s1.name=’zhangfei’#自由的给对象绑定属性 
print s1.name 
class teacher(object): 
    “”“docstring for teacher”“” 
    def init(self,name,age): 
        super(teacher, self).init()
    self.name=name
    self.age=age
def print_info(self):
    print(‘%s: %s‘ % (self.name, self.age))
t2=teacher(‘咋就’,23) 
print t2.age 
t2.print_info()#类中定义的函数只有一点不同,就是第一个参数永远是实例变量self,并且,调用时,不用传递该参数 
class Animal(object): 
    def run(self): 
        print(‘Animal is running…’)
class Dog(Animal):
def run(self):
    print(‘Dog is running...‘)
class Cat(Animal):
def run(self):
    print(‘Cat is running...‘)
def run_twice(animal): 
        animal.run() 
        animal.run() 
run_twice(Animal()) 
run_twice(Cat()) 
class Tortoise(Animal): 
    def run(self): 
        print(‘Tortoise is running slowly…’) 
run_twice(Tortoise())
标签:
原文地址:http://blog.csdn.net/qq_26992267/article/details/51333304