Esempio n. 1
0
    if type(sup) is Superhero:
        print('I am a superhero')

    # Get the Method Resolution search Order used by both getattr() and super()
    # This attribute is dynamic and can be updated
    print(Superhero.__mro__)  # => (<class '__main__.Superhero'>,
    # => <class 'human.Human'>, <class 'object'>)

    # Calls parent method but uses its own class attribute
    print(sup.get_species())  # => Superhuman

    # Calls overridden method
    print(sup.sing())  # => Dun, dun, DUN!

    # Calls method from Human
    sup.say('Spoon')  # => Tick: Spoon

    # Call method that exists only in Superhero
    sup.boast()  # => I wield the power of super strength!
    # => I wield the power of bulletproofing!

    # Inherited class attribute
    sup.age = 31
    print(sup.age)  # => 31

    # Attribute that only exists within Superhero
    print('Am I Oscar eligible? ' + str(sup.movie))

####################################################
## 6.2 Multiple Inheritance
####################################################
Esempio n. 2
0
    if type(sup) is Superhero:
        print('I am a superhero')

    # Get the Method Resolution search Order used by both getattr() and super()
    # This attribute is dynamic and can be updated
    print(Superhero.__mro__)    # => (<class '__main__.Superhero'>,
                                # => <class 'human.Human'>, <class 'object'>)

    # Calls parent method but uses its own class attribute
    print(sup.get_species())    # => Superhuman

    # Calls overloaded method
    print(sup.sing())           # => Dun, dun, DUN!

    # Calls method from Human
    sup.say('Spoon')            # => Tick: Spoon

    # Call method that exists only in Superhero
    sup.boast()                 # => I wield the power of super strength!
                                # => I wield the power of bulletproofing!

    # Inherited class attribute
    sup.age = 31
    print(sup.age)              # => 31

    # Attribute that only exists within Superhero
    print('Am I Oscar eligible? ' + str(sup.movie))

####################################################
## 6.2 Multiple Inheritance
####################################################
        print('Я супергерой')

    # Получить порядок поиска разрешения метода (MRO),
    # используемый как getattr(), так и super()
    # Этот атрибут является динамическим и может быть обновлен
    print(Superhero.__mro__)    # => (<class '__main__.Superhero'>,
                                # => <class 'human.Human'>, <class 'object'>)

    # Вызывает родительский метод, но использует свой собственный атрибут класса
    print(sup.get_species())    # => Сверхчеловек

    # Вызов переопределенного метода
    print(sup.sing())           # => Бам, бам, БАМ!

    # Вызывает метод из Human
    sup.say('Ложка')            # => Тик: Ложка

    # Метод вызова, существующий только в Superhero
    sup.boast()                 # => Я обладаю силой 'сверхсила'!
                                # => Я обладаю силой 'пуленепробиваемость'!

    # Атрибут унаследованного класса
    sup.age = 31
    print(sup.age)              # => 31

    # Атрибут, который существует только в Superhero
    print('Достоин ли я Оскара? ' + str(sup.movie))


####################################################
## 6.2 Множественное наследование
    if type(sup) is Superhero:
        print("I am a superhero")

    # Get the Method Resolution search Order used by both getattr() and super()
    # This attribute is dynamic and can be updated
    print(Superhero.__mro__)  # => (<class '__main__.Superhero'>,
    # => <class 'human.Human'>, <class 'object'>)

    # Calls parent method but uses its own class attribute
    print(sup.get_species())  # => Superhuman

    # Calls overridden method
    print(sup.sing())  # => Dun, dun, DUN!

    # Calls method from Human
    sup.say("Spoon")  # => Tick: Spoon

    # Call method that exists only in Superhero
    sup.boast()  # => I wield the power of super strength!
    # => I wield the power of bulletproofing!

    # Inherited class attribute
    sup.age = 31
    print(sup.age)  # => 31

    # Attribute that only exists within Superhero
    print("Am I Oscar eligible? " + str(sup.movie))

####################################################
## 6.2 Multiple Inheritance
####################################################