首页 > 代码库 > 继承和多态

继承和多态

类可以继承自基类,也可以被子类继承,比如Animal继承自object类,而自身又可以被Dog和Cat类继承

class Animal(object):
    def run(self):
        print(‘Animal is running‘)
class Dog(Animal):
  pass
class Cat(Animal):
  pass  

继承的子类拥有了基类的全部属性和方法,而且子类还可以在基类的基础上新增属性或者方法,如下代码在printscore报错

class Student(object):
    def __init__(self,name,score):
        self.__name=name
        self.__score=score
    
    def printscore(self):
        print(‘%s\‘s score is %s‘%(self.__name,self.__score))
        
class Junior(Student):
    def __init__(self,name,score,sex,height,weight):
        self.__name=name
        self.__score=score
        self.__sex=sex
        self.__height=height
        self.__weight=weight
    def printstudent(self):
        print(‘%s %s %s %s %s‘%(self.__name,self.__score,self.__sex,self.__height,self.__weight))
class Master(Student):
    pass

kimi=Junior(‘kimi‘,100,‘M‘,180,65)
kimi.printscore()
kimi.printstudent()

报错的原因在于Junior重写了初始化函数,因此Junior的实例中__name被更名为_Junior__name,而Student中的__name被更名为_Student__name,而printscore中调用的是_Student__name,导致调用失败。通过本例理解了__打头的变量就连自己的子类都无法调用。

上例正确的用法应该是  

class Student(object):
    def __init__(self,name,score):
        self.name=name
        self.score=score
    
    def printscore(self):
        print(‘%s\‘s score is %s‘%(self.name,self.score))
        
class Junior(Student):
    def __init__(self,name,score,sex,height,weight):
        self.name=name
        self.score=score
        self.sex=sex
        self.height=height
        self.weight=weight
    def printstudent(self):
        print(‘%s %s %s %s %s‘%(self.name,self.score,self.sex,self.height,self.weight))
class Master(Student):
    pass

kimi=Junior(‘kimi‘,100,‘M‘,180,65)
kimi.printscore()
kimi.printstudent()

还可以这么定义Junior类:

class Junior(Student):
    def __init__(self,name,score,sex,height,weight):
        Student.__init__(self,name,score)#或者使用super(Junior,self).__init__(self,name,score)
        self.sex=sex
        self.height=height
        self.weight=weight
    def printstudent(self):
        print(‘%s %s %s %s %s‘%(self.name,self.score,self.sex,self.height,self.weight))

 子类可以覆盖基类的方法和属性,如果一个函数的参数为基类,我们就可以传入子类作为参数,因为子类属于基类,而且函数并不因为传入了子类而需要修改,对于一个变量,我们只需要知道它的基类,而具体的细节由它具体的子类来决定。允许新增子类,而不需要修改依赖该基类的函数。

 

  

 

 

 

  

 

继承和多态