简体   繁体   English

缺少 2 个必需的位置 arguments

[英]missing 2 required positional arguments

My code wont work and it keeps repeating these error.我的代码无法正常工作,并且不断重复这些错误。 "TypeError: years_of_employment() missing 2 required positional arguments: 'self' and 'current_year. “TypeError:years_of_employment() 缺少 2 个必需的位置 arguments:‘self’ 和 ‘current_year。
What I tried to do was create a class that inherited from superclass and try information hiding.我试图做的是创建一个从超类继承的 class 并尝试隐藏信息。 Here's the code.这是代码。

class Person:
    def __init__(self, first_name, last_name, birth_year, salary, employed_year):
        self.__first_name = first_name
        self.__last_name = last_name
        self.__birth_year = birth_year
        self.__salary = salary
        self.__employed_year = employed_year
    
    @property
    def first_name(self):
        return self.__first_name
    @property
    def last_name(self):
        return self.__last_name
    @property
    def birth_year(self):
        return self.__birth_year
    @property
    def salary(self):
        return self.__salary
    @property
    def employed_year(self):
        return self.__employed_year
    
    def age(self, current_year):
        return f'{current_year - self.birth_year}'        

class Teacher(Person):
    def __init__(self, first_name, last_name, birth_year, salary, employed_year):
        super().__init__(first_name, last_name, birth_year)
        self.salary = salary
        self.employed_year = employed_year

    def __str__(self):
        return f'{self.first_name} {self.last_name} är född {self.birth_year} och tjäner {self.salary} kr/månad'
        
    def years_of_employment(self, current_year):
        return (current_year - self.employed_year) * '*'

person = Person("Jan", "Gran", 1986, 28000 , 2015)
Teacher.years_of_employment()

print(f'{person.first_name} is born {person.birth_year} and earn {person.salary} SEK/month')
print(f'Teacher {person.first_name} is {person.age(2022)} old')
print(f'Number of years of employment: {Teacher.years_of_employment(2022)}')

The answer should be this:答案应该是这样的:
Jan Gran was born in 1986 and earns SEK 28,000 / month The teacher Jan Gran 1986年出生,月收入28,000瑞典克朗老师
Jan is 36 years old Number of years of employment: Jan 36 岁工作年限:

Like the error says, you haven't provided a Teacher ( self ) or the current_year .如错误所述,您没有提供Teacher ( self ) 或current_year

Create a Teacher , not a Person :创建一个Teacher ,而不是一个Person

person = Teacher("Jan", "Gran", 1986, 28000 , 2015)

and then you can call its years_of_employment method, passing the current year as the argument:然后你可以调用它的years_of_employment方法,将当前年份作为参数传递:

print(person.years_of_employment(2022))

Note that you have a bug in Teacher.__init__ that will raise another exception when you try to create a Teacher , which is that you don't pass all the necessary parameters to Person.__init__ .请注意,您在Teacher.__init__中有一个错误,当您尝试创建Teacher时会引发另一个异常,即您没有将所有必要的参数传递给Person.__init__ But since your Teacher doesn't init anything that Person doesn't, you don't need to define a Teacher.__init__ at all -- and all the stuff in Person.__init__ plus all its properties can be implemented automatically by decorating it as a dataclass and just listing all the properties:但是因为你的Teacher没有初始化任何Person没有的东西,你根本不需要定义Teacher.__init__ - Person.__init__中的所有东西加上它的所有属性都可以通过装饰它来自动实现一个dataclass ,只列出所有属性:

from dataclasses import dataclass

@dataclass(frozen=True)
class Person:
    first_name: str
    last_name: str
    birth_year: int
    salary: int
    employed_year: int
    
    def age(self, current_year: int) -> str:
        return f'{current_year - self.birth_year}'        


class Teacher(Person):
    def __str__(self) -> str:
        return f'{self.first_name} {self.last_name} är född {self.birth_year} och tjäner {self.salary} kr/månad'
        
    def years_of_employment(self, current_year: int) -> str:
        return (current_year - self.employed_year) * '*'


person = Teacher("Jan", "Gran", 1986, 28000 , 2015)
print(f'{person.first_name} is born {person.birth_year} and earn {person.salary} SEK/month')
print(f'Teacher {person.first_name} is {person.age(2022)} old')
print(f'Number of years of employment: {person.years_of_employment(2022)}')

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

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