简体   繁体   English

Python Inheritance - 将用户输入的值从父母传递给孩子 class

[英]Python Inheritance - Carrying a user inputted value from parent to child class

class Tax():

    def __init__(self,user_pre_tax, after_federal_tax_amount):
        self.user_pre_tax = user_pre_tax
        self.after_federal_tax_amount = after_federal_tax_amount
    def basic_information():
        federal_tax_rate = 0.10
        user_name = (input("Enter name: "))
        user_pre_tax = (int(input("Enter pre tax amount: ")))
        federal_tax_amount = user_pre_tax * federal_tax_rate
        after_federal_tax_amount = user_pre_tax - federal_tax_amount
        print ("Federal Tax Amount: ", federal_tax_amount)

class OregonTax(Tax):
    def __init__(self,user_pre_tax, after_federal_tax_amount):
        super().__init__(user_pre_tax, after_federal_tax_amount)
    def OregonTax():
        oregon_tax_rate = 0.03
        oregon_tax_amount = Tax.user_pre_tax * oregon_tax_rate
        after_oregon_tax_amount = Tax.after_federal_tax_amount - oregon_tax_amount
        print ("Oregon Tax Amount: ", oregon_tax_amount)

class WashingtonTax(Tax):
    def __init__(self,user_pre_tax, after_federal_tax_amount):
        super().__init__(user_pre_tax, after_federal_tax_amount)
    def OregonTax():
        wa_tax_rate = 0.04
        wa_tax_amount = Tax.user_pre_tax * wa_tax_rate
        after_oregon_tax_amount = Tax.after_federal_tax_amount - wa_tax_amount
        print ("Oregon Tax Amount: ", wa_tax_amount)


example1 = OregonTax
OregonTax.basic_information()
OregonTax.OregonTax()

I am trying to carrying the "after_federal_tax_amount" from the parent to child class. Since it is a user inputted value, I don't think I can place it in the the init.我正在尝试将“after_federal_tax_amount”从父项传递给子项 class。由于它是用户输入的值,我认为我不能将它放在 init 中。 Otherwise, when I initialize the class at the end, I would need to enter a value as its parameter.否则,当我最后初始化 class 时,我需要输入一个值作为它的参数。

I was wondering how I could by pass this problem.我想知道如何解决这个问题。

First of all, there is a numerous problems with your classes structure.首先,您的类结构存在很多问题。 in the function OregonTax you are trying to access Tax.user_pre_tax and Tax.after_federal_tax_amount , whilst both of them are actually do not exist for an abstract class, but exists per instance only, if you want to be able to access them like that you would need to assign those values outside the init function per class instance.在 function OregonTax 中,您正在尝试访问Tax.user_pre_taxTax.after_federal_tax_amount ,而对于抽象 class 实际上它们都不存在,但仅存在于每个实例中,如果您希望能够像那样访问它们需要在每个 class 实例的init function 之外分配这些值。 But I don't really see a purpose of that, since you already create such attributes for both Oregon and Washington classes and if you pass self to the OregonTax function, you would be able to access it by calling self.after_federal_tax_amount and there is no particular need to access parent class in any way.但我真的看不出这样做的目的,因为你已经为俄勒冈和华盛顿类创建了这样的属性,如果你将 self 传递给 OregonTax function,你将能够通过调用 self.after_federal_tax_amount 来访问它并且没有特别需要以任何方式访问父 class。

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

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