-2

Why?

What I want to show is:

====User Data====
User Name : Tom
User Age : 22
User Address : shanghai

Tips:It looks like your post is mostly code; please add some more details. So,

class A:
    def __init__(self, userName=''):
        self.userName = userName
    def showInfo(self):
        print(f'User Name:{self.userName}')


class B:
    def __init__(self, userAge=0):
        self.userAge = userAge
    def showInfo(self):
        print(f'User Age:{self.userAge}')


class C(A, B):
    def __init__(self, userName='', userAge=0, userAddress=''):
        super(C, self).__init__(userName)
        super(C, self).__init__(userAge)
        self.userAddress = userAddress

    def showInfo(self):
        print('====User Data====')
        super(C, self).showInfo()
        super(C, self).showInfo()
        print(f'User Address:{self.userAddress}')

# why User Name 22 ?
# ====User Data====
# User Name:22
# User Name:22
# User Address:shanghai
tom = C('Tom', 22, 'shanghai')
tom.showInfo()
rv.kvetch
  • 9,940
  • 3
  • 24
  • 53
  • Both calls to `super().__init__()` are going to call the same base class, and the other base class doesn't get initialized at all. Likewise, both calls to `super().showInfo()` call the same base class's implementation. `super()` simply does not get along with the combination of multiple inheritance, and differing `__init__()` parameter lists (any two of those three things works just fine) - consider explicitly naming the base class you want to call in each place where you're currently using `super()`. – jasonharper Aug 16 '22 at 02:56

1 Answers1

0

Try this:

class A:
    def __init__(self, userName=''):
        self.userName = userName
    def showInfo(self):
        print(f'User Name:{self.userName}')


class B:
    def __init__(self, userAge=0):
        self.userAge = userAge
    def showInfo(self):
        print(f'User Age:{self.userAge}')


class C(A, B):
    def __init__(self, userName='', userAge=0, userAddress=''):
        A.__init__(self, userName)
        B.__init__(self, userAge)
        self.userAddress = userAddress

    def showInfo(self):
        print('====User Data====')
        A.showInfo(self)
        B.showInfo(self)
        print(f'User Address:{self.userAddress}')

tom = C('Tom', 22, 'shanghai')
tom.showInfo()
rv.kvetch
  • 9,940
  • 3
  • 24
  • 53