简体   繁体   中英

Changing a class value of a class attribute with default 0 through instance value

I am working with a certain script that calculates discount, where its default is 0, hwoever special items have varied discounts, and my challenge is that I am unable top update the discount. Here's a sample code:

class Person():
    def __init__(self, item, quantity, money,discount=0):
        self.discount=discount
        self.item=item
        self.quantity=quantity
        self.money=money
        if self.money < quantity*1000:
            print('Not enough money')
        else:
            self.quantity=quantity
            if discount == 0:
                self.money=self.money-self.quantity*1000
            else:
                self.money=self.money-self.quantity*1000*(1-discount)

class Privilage(Person):
    def __init__(self, item, quantity, money, tag):
        super().__init__(item, quantity, money,)
        self.tag=tag
        if self.tag == 'vip':
            self.discount=0.1
        elif self.tag == 'vvip':
            self.discount=0.2
        else:
            self.discount=0

I tried changing the numbers and checking outputs by printing self.money, but they always pass trhough discount == 0 instead on the else, whcihc should carry over the discount by class Privilage. I also tried adding other methods, and it works, it simply won't pass in the class Person.

I think your problem here is that you are trying to define the attributes of the superclass Person by the subclass Privilage. The subclass will inherit any attributes and methods from the superclass, but not vice versa.

A solution would be to move the if-else loop from Person to the Privilage class and then it works.

class Person():
    def __init__(self, item, quantity, money,discount=0):
        self.discount=discount
        self.item=item
        self.quantity=quantity
        self.money=money
                    
class Privilage(Person):
    def __init__(self, item, quantity, money, tag):
        super().__init__(item, quantity, money,)
        self.tag=tag
        
        # if loop to determine discount status
        if self.tag == 'vip':
            self.discount=0.1
        elif self.tag == 'vvip':
            self.discount=0.2
        else:
            self.discount=0
        
        # if loop to check money with discount status
        if self.money < quantity*1000:
            print('Not enough money')
        else:
            self.quantity=quantity
            if self.discount == 0:
                self.money=self.money-self.quantity*1000
            else:
                self.money=self.money-self.quantity*1000*(1-self.discount)
        

print('-------------------------------------------------------')
bob = Privilage(item='jacket', quantity=4, money=50000, tag='vip')
print("Bob has:", bob.discount, bob.money)

sue = Privilage(item='jacket', quantity=5, money=4000, tag=0)
print("Sue has:", sue.discount, sue.money)

john = Privilage(item='jacket', quantity=10, money=100000, tag='vvip')
print("John has:", john.discount, john.money)

Resulting output:

-------------------------------------------------------
Bob has: 0.1 46400.0
Not enough money
Sue has: 0 4000
John has: 0.2 92000.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM