zoukankan      html  css  js  c++  java
  • python-继承,父类,子类

    class Spell(object):
        def __init__(self, incantation, name):
            self.name = name
            self.incantation = incantation
    
        def __str__(self):
            return self.name + ' ' + self.incantation + '
    ' + self.getDescription()
                  
        def getDescription(self):
            return 'No description'
        
        def execute(self):
            print(self.incantation)
    
    
    class Accio(Spell):
        def __init__(self):
            Spell.__init__(self, 'Accio', 'Summoning Charm')
    
    class Confundo(Spell):
        def __init__(self):
            Spell.__init__(self, 'Confundo', 'Confundus Charm')
    
        def getDescription(self):
            return 'Causes the victim to become confused and befuddled.'
    
    def studySpell(spell):
        print(spell)
    
    spell = Accio()
    spell.execute()
    studySpell(spell)
    studySpell(Confundo())

    输出:

    Accio
    Summoning Charm Accio
    No description
    Confundus Charm Confundo
    Causes the victim to become confused and befuddled.


    class A(object):
        def __init__(self):
            self.a = 1
        def x(self):
            print("A.x")
        def y(self):
            print("A.y")
        def z(self):
            print("A.z")
    
    class B(A):
        def __init__(self):
            A.__init__(self)
            self.a = 2
            self.b = 3
        def y(self):
            print("B.y")
        def z(self):
            print("B.z")
    
    class C(object):
        def __init__(self):
            self.a = 4
            self.c = 5
        def y(self):
            print("C.y")
        def z(self):
            print("C.z")
    
    class D(C, B):
        def __init__(self):
            C.__init__(self)
            B.__init__(self)
            self.d = 6
        def z(self):
            print("D.z")

    obj = D()
    print(obj.a)
    print(obj.b)
    print(obj.c)
    print(obj.d)

    obj.x()

    obj.y()

    obj.z()

    输出:

    2
    3
    5
    6
    A.x
    C.y
    D.z
  • 相关阅读:
    币值转换
    第八周作业
    第七周作业
    第五周编程总结
    第四周编程总结
    第三周编程总结
    7-1 查找整数
    7-2 求最大值及其下标
    秋季学习总结
    对我影响最大的三个老师
  • 原文地址:https://www.cnblogs.com/Bella2017/p/8024813.html
Copyright © 2011-2022 走看看