zoukankan      html  css  js  c++  java
  • python--继承关系

    如果子类中定义与父类同名的方法或属性,则自动会覆盖父类对应的方法或属性。

    子类完全继承父类的实例

    >>> class Parent:
    def setName(self):
    print("正在调用父类的定义方法")


    >>> class Child(Parent):
    pass

    >>> p = Parent()
    >>> p.setName()
    正在调用父类的定义方法
    >>>
    >>>
    >>> p= Child()
    >>> p.setName()
    正在调用父类的定义方法

    子类自定义和父类相同的方法或属性

    >>> class Parent:
    def setName(self):
    print("正在调用父类的定义方法")

    >>>
    >>> class Child(Parent):
    def setName(self):
    print("正在调用子类自己的定义方法")


    >>> p = Parent()
    >>> p.setName()
    正在调用父类的定义方法
    >>> p= Child()
    >>> p.setName()
    正在调用子类自己的定义方法
    >>>

     子类自定义属性方法同时又需要用到父类的同名的属性方法的解决方法:

    子类自定义属性方法和父类属性方法同名的实例

    import random as r

    class Dog:
    def __init__(self):
    self.x = r.randint(0,10)
    self.y = r.randint(0,10)


    def move(self):
    self.x -= 1
    print("我的位置是:",self.x, self.y)

    class Golddog(Dog):
    pass

    class Carpdog(Dog):
    pass

    class Saldog(Dog):
    pass

    class Sharkdog(Dog):
    def __init__(self):
    self.hungry = True

    def eat(self):
    if self.hungry:
    print("吃货的世界就是这么简单")
    self.hungry = False

    else:
    print("吃饱了,好想睡一觉!!!")

    实现代码的过程

    >>> dog = Dog()
    >>> dog.move()
    我的位置是: 9 7
    >>> dog.move()
    我的位置是: 8 7
    >>> dog.move()
    我的位置是: 7 7

    >>> dog.move()
    我的位置是: 6 7
    >>> golddog = Golddog()
    >>> golddog.move()
    我的位置是: 0 3
    >>> golddog.move()
    我的位置是: -1 3

    =========================== RESTART: D:/test/继承.py ===========================
    >>> sharkdog = Sharkdog()

    >>> sharkdog.eat()
    吃货的世界就是这么简单
    >>> sharkdog.eat()
    吃饱了,好想睡一觉!!!
    >>> sharkdog.move()
    Traceback (most recent call last):
    File "<pyshell#60>", line 1, in <module>
    sharkdog.move()
    File "D:/test/继承.py", line 10, in move
    self.x -= 1
    AttributeError: 'Sharkdog' object has no attribute 'x'
    >>>

    在调用sharkdog.move()这个对象方法是报错异常,是因为子类改写了父类的init方法,不在继承父类的方法

    如果想解决上面的报错问题可以有2种方法:

     1) 调用未绑定的父类方法

    改写的代码如下:

    import random as r

    class Dog:
    def __init__(self):
    self.x = r.randint(0,10)
    self.y = r.randint(0,10)


    def move(self):
    self.x -= 1
    print("我的位置是:",self.x, self.y)

    class Golddog(Dog):
    pass

    class Carpdog(Dog):
    pass

    class Saldog(Dog):
    pass

    class Sharkdog(Dog):
    def __init__(self):
    Dog.__init__(self)     ##添加这么一行代码
    self.hungry = True

    def eat(self):
    if self.hungry:
    print("吃货的世界就是这么简单")
    self.hungry = False

    else:
    print("吃饱了,好想睡一觉!!!")

    代码执行结果:

    >>> sharkdog = Sharkdog()
    >>> sharkdog.move()
    我的位置是: 8 0
    >>> sharkdog.move()
    我的位置是: 7 0
    >>>

    2)使用super函数

    代码如下:

    import random as r

    class Dog:
    def __init__(self):
    self.x = r.randint(0,10)
    self.y = r.randint(0,10)


    def move(self):
    self.x -= 1
    print("我的位置是:",self.x, self.y)

    class Golddog(Dog):
    pass

    class Carpdog(Dog):
    pass

    class Saldog(Dog):
    pass

    class Sharkdog(Dog):
    def __init__(self):

    # super(FooChild,self) 首先找到 FooChild 的父类(就是类 FooParent),然后把类B的对象 FooChild 转换为类 FooParent 的对象
    super().__init__()   ##添加代码super函数
    self.hungry = True

    def eat(self):
    if self.hungry:
    print("吃货的世界就是这么简单")
    self.hungry = False

    else:
    print("吃饱了,好想睡一觉!!!")

    代码执行结果:

    >>> sharkdog = Sharkdog()
    >>> sharkdog.move()
    我的位置是: 3 3
    >>> sharkdog.move()
    我的位置是: 2 3
    >>>

    ===============================================

    super函数的作用 

    super() 函数是用于调用父类(超类)的一个方法。

    super 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是如果使用多继承,会涉及到查找顺序(MRO)、重复调用(钻石继承)等种种问题

    super函数可以不用指定父类的名字  自己去逐层查看符合条件的父类的名字 

  • 相关阅读:
    学习心得总结(1)
    Git使用总结
    Git管理分支
    Git 常用命令整理
    C#Windows窗体中添加了AxWindowsMediaPlayer的详细用法影响键盘操作的问题
    连接SQLsever数据库在C#中不能操作的问题
    C#中字符串的操作
    iOS TableView如何刷新指定的cell或section
    iOS所有的子视图
    iOStextFiled判断输入长度
  • 原文地址:https://www.cnblogs.com/pyng/p/10057618.html
Copyright © 2011-2022 走看看