简体   繁体   English

通过实例值更改默认为 0 的 class 属性的 class 值

[英]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.我正在使用某个计算折扣的脚本,它的默认值为 0,任何特殊商品都有不同的折扣,我的挑战是我无法更新折扣。 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.我尝试通过打印 self.money 更改数字并检查输出,但它们总是通过 discount == 0 而不是 else,whcihc 应该通过 class 特权传递折扣。 I also tried adding other methods, and it works, it simply won't pass in the class Person.我还尝试添加其他方法,它有效,它根本不会传递 class Person。

I think your problem here is that you are trying to define the attributes of the superclass Person by the subclass Privilage.我认为你的问题是你试图通过子类 Privilage 定义超类 Person 的属性。 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.一个解决方案是将 if-else 循环从 Person 移动到权限 class 然后它就可以工作了。

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:结果 output:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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