Python——继承

一. 继承

定义一个  class  的时候,可以从某个现有的  class  继承,新的 class  称为子类(Subclass),而被继承的 class  称为基类、父类或超类(Base class、Super class)

继承的定义:

class DerivedClassName(BaseClassName1):

        <statement-1>

        .

        .

         <statement-N>

BaseClassName(示例中的基类名)必须与派生类定义在一个作用域内。除了类,还可以用表达式,基类定义在另一个模块中时这一点非常有用:

   class DerivedClassName(modname.BaseClassName):

Python 中继承的特点:

  • 在继承中,基类的构造方法(__init__()方法)不会自动调用,需要在子类的构造方法中专门调用。
  • 在调用基类的方法时需要加上基类的类名前缀,并带上  self  参数变量。区别于在类中调用普通函数时不需要带  self  参数。
  • 在  Python  中,首先查找对应类型的方法,如果在子类中找不到对应的方法,才到基类中逐个查找。
#!/usr/bin/python3

# 类定义

class people:
    # 定义基本属性
    name = ''
    age = 0
    # 定义私有属性,私有属性在类外部无法直接进行访问
    __weight = 0
    # 定义构造方法
    def __init__(self,n,a,w):
        self.name = n
        self.age = a
        self.__weight = w
    
    def speak(self):
        print("%s 说:我  %d 岁了"%(self.name,self.age))
        
        
# 单继承
class student (people):
    grade = ''
    def __init__(self,n,a,w,g):
        # 调用父类的构造
        people.__init__(self, n, a, w)
        self.grade = g 
        
    # 覆写父类法人方法
    def speak(self):
        print("%s 说:我  %d 岁了,在读 %d 年级"%(self.name,self.age,self.grade))
        
zth = student('zth',10,40,5)
zth.speak()

执行结果:

zth 说:我  10 岁了,在读 5 年级
  • 子类获得了父类全部非私有的功能。
  • 子类不能继承父类中的私有方法,也不能被调用父类的私有方法。
  • 对于父类中扩展的非私有方法,子类可以拿来即用。
>>> class Animal(object):
...     def run(self):
...             print('animal is running ...')
...     def __run(self):
...             print(' I am a private method.')
...
>>>
>>> class Dog(Animal):
...     def eat(self):
...             print(' dog is eating ')
...
>>>
>>> dog = Dog()
>>>
>>> dog.run()
animal is running ...
>>> dog.eat()
 dog is eating
>>> dog.__run()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Dog' object has no attribute '__run'
>>>

继承树:

           

二. 多重继承

多重继承的定义:

class DerivedClassName(Base1, Base2, Base3):

        <statement-1>

        .

        .

        <statement-N>

  • 多重继承就是有多个父类的继承。
  • 需要注意圆括号中父类的顺序,若是父类中有相同的方法名,而在子类使用时未指定,python从左至右搜索 即方法在子类中未找到时,从左到右查找父类中是否包含方法。
#!/usr/bin/python3

# -*-coding:UTF-8-*-

 
#类定义
class people:
    #定义基本属性
    name = ''
    age = 0
    #定义私有属性,私有属性在类外部无法直接进行访问
    __weight = 0
    #定义构造方法
    def __init__(self,n,a,w):
        self.name = n
        self.age = a
        self.__weight = w
    def speak(self):
        print("%s 说: 我 %d 岁。" %(self.name,self.age))

#单继承示例
class student(people):
    grade = ''
    def __init__(self,n,a,w,g):
        #调用父类的构函
        people.__init__(self,n,a,w)
        self.grade = g
    #覆写父类的方法
    def speak(self):
        print("%s 说: 我 %d 岁了,我在读 %d 年级"%(self.name,self.age,self.grade))

# 类定义
class speaker():
    topic = ''
    name = ''
    def __init__(self,n,t):
        self.name = n 
        self.topic = t
    def speak(self):
        print("我叫 %s,我是一个演说家,我演讲的主题是 %s"%(self.name,self.topic))
        

# 多重继承

class ZTH(speaker,student):
    a = ''
    def __init__(self,n,a,w,g,t):
        student.__init__(self, n, a, w, g)
        speaker.__init__(self, n, t)


zth = ZTH('zth',10,40,5,'Python3')

zth.speak() #方法名同,默认调用的是在括号中排前地父类的方法

执行结果:

我叫 zth,我是一个演说家,我演讲的主题是 Python3

猜你喜欢

转载自blog.csdn.net/qq_41573234/article/details/82053752