|  | 
 
| 
知识点:
x
马上注册,结交更多好友,享用更多功能^_^您需要 登录 才可以下载或查看,没有账号?立即注册  语法:
 class DerivedClassName(BaseClassName)
 子类名                     父类名
 
 一、一个子类可以继承它父类的所有属性和方法。
 例:
 
 复制代码class Parent():
    def hello(self):
        print("你好啊~")
class Child(Parent):        #孩子类继承父亲类。
    pass
p = Parent()
p.hello()
c = Child()
c.hello()                #孩子类的对象同样可以调用父亲类的方法。
你好啊~                #父类对象调用方法后输出的结果
你好啊~                #子类对象调用方法后输出的结果
 
 二、如果子类中定义与父类同名的方法或属性,则会自动覆盖父类对应的方法或属性。
 例:
 
 复制代码class Parent():
    def hello(self):
        print("你好啊~")
class Child(Parent):
    def hello(self):                #在子类中定义了与父类重名的方法。
        print("^_^")
    pass
p = Parent()
p.hello()
c = Child()
c.hello()
你好啊~
^_^                        #子类对象调用方法后输出的结果与父类不一致。
 
 三、调用未绑定的父类方法和super()函数
 
 先看例子(不懂看注释啊^_^)
 
 复制代码import random as r
class Fish():                        #在此处定义父类。
    def __init__(self):
        self.x = r.randint(0,10)
        self.y = r.randint(0, 10)
        print("我现在的位置是【%d,%d】" %(self.x,self.y))
    def move(self):
        self.x -= 1
        print("我移动【%d,%d】的位置了" %(self.x,self.y))
class Shark(Fish):                #定义子类
    hunger = True
    def __init__(self):                #在这里重写了__init__()方法。
        if self.hunger:
            print("吃的好饱啊!")
            self.hunger = False
        else:
            print("不能再吃了,撑死了!")
s = Shark()                        #实例化子类对象
s.move()                                #调用继承到的方法。
#下面的输出结果是异常。
吃的好饱啊!
Traceback (most recent call last):
  File "E:/Python/第038讲.类和对象:继承/实验.py", line 48, in <module>
    s.move()
  File "E:/Python/第038讲.类和对象:继承/实验.py", line 23, in move
    self.x -= 1
AttributeError: 'Shark' object has no attribute 'x'
在上面的例子中虽然子类继承了父类,但是调用父类中的方法却报错了。原因是因为,在子类中重写了__init__()方法,所以导致了方法move()中缺少参数。
 
 解决方法有两种:
 1)调用未绑定的父类方法
 用父类对象的对象名调用父类中的__init__()方法,参数却为子类的实例化对象(此时调用的方法就叫未绑定的父类方法)。这样就可以实现即重写父类中的方法,又可以使用父类中的方法。
 
 复制代码class Shark(Fish):
    hunger = True
    def __init__(self):
        Fish.__init__(self)                #在这里用父类对象调用父类中的__init__()方法。
        if self.hunger:
            print("吃的好饱啊!")
            self.hunger = False
        else:
            print("不能再吃了,撑死了!")
其实就相当于这样写:
其实就相当于这样写:
 
 复制代码s = Shark()
Fish.__init__(s)
s.move()
 2)使用super()函数
 使用super()函数可以帮我们自动找到父类的方法,并且自动传入self(也就是子类对象)参数。
 supper()函数的超级之处就在于,你不用给任何父类的名字(可能存在多个父类或者有好多个祖先类),它会自动帮你一层层找出它所有父类中对应的方法。
 由于你不用给出父类的名字,这就意味着你如果需要改变了类继承关系,你只要改变class语句里的父类即可,而不必在大量代码中去修改所有被继承的方法。
 语法:
 super().需要的父类的方法名()                #注意括号内不用加self
 
 复制代码class Shark(Fish):
    hunger = True
    def __init__(self):
        super().__init__()
        if self.hunger:
            print("吃的好饱啊!")
            self.hunger = False
        else:
            print("不能再吃了,撑死了!")
 
 
 四、多重继承
 多重继承看起来很厉害,但是很容易导致代码混乱。所以尽量避免使用它。
 
 复制代码class A:
    def __init__(self):
        print("我是类A")
class B:
    def __init__(self):
        print("我是类B")
class C(A,B):                #类C同时继承类A和类B
    pass
c = C()
#下面是输出结果
我是类A                                #如果父类中的方法名相同那么会调用前面的那个类中的方法。
 | 
 评分
查看全部评分
 |