“Python Override odziedziczona metoda konstruktor klasy klasy” Kod odpowiedzi

Python Override odziedziczona metoda konstruktor klasy klasy

class Bar(object):
   def __init__(self, arg1=None, arg2=None, argN=None):
       print arg1, arg2, argN

class Foo(Bar):
    def __init__(self, my_new_arg=None, **kwds):
       super(Foo, self).__init__(**kwds)
       self.new_arg = my_new_arg
       print my_new_arg

f = Foo(my_new_arg='x', arg2='y')
DreamCoder

Python Override odziedziczona metoda konstruktor klasy klasy

class Parent(object):
    def __init__(self, a, b):
        print 'a', a
        print 'b', b

class Child(Parent):
    def __init__(self, c, d, *args, **kwargs):
        print 'c', c
        print 'd', d
        super(Child, self).__init__(*args, **kwargs)

test = Child(1,2,3,4)
DreamCoder

Metoda odziedziczona Pythona

import datetime

class Logger(object):
    def log(self, message):
        print message

class TimestampLogger(Logger):
    def log(self, message):
        message = "{ts} {msg}".format(ts=datetime.datetime.now().isoformat(),
                                      msg=message)
        super(TimestampLogger, self).log(message)
DreamCoder

Odpowiedzi podobne do “Python Override odziedziczona metoda konstruktor klasy klasy”

Pytania podobne do “Python Override odziedziczona metoda konstruktor klasy klasy”

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu