class 学生的计算

Calculations in class Student

我在不同的模块中创建了两个 classes:Person 和 Student。这是我的 class 人:

import datetime


class Person:

    def __init__(self, name, surname, patronymic, birthdate):
        self.name = name
        self.surname = surname
        self.patronymic = patronymic
        self.birthdate = birthdate

    def age(self):#this function calculates age of person
        today = datetime.date.today()
        age = today.year - self.birthdate.year

        if today < datetime.date(today.year, self.birthdate.month, self.birthdate.day):
            age -= 1

        return age

这是我的 class 学生:

from ClassPerson import Person


class Student(Person):
    number_of_group = eval(input("\nInput number of group: "))
    summa = 0
    amount_of_students = 0
    overall_age = 0

    def __init__(self, name, surname, patronymic, birthdate, faculty, group, scholarship):
        Person.__init__(self, name, surname, patronymic, birthdate)
        self.faculty = faculty
        self.group = group
        self.scholarship = scholarship
        if Student.number_of_group == self.group:
            Student.summa += self.scholarship
            Student.overall_age += Student.age(self)
            Student.amount_of_students += 1

    @property
        def scholarship(self):
            return self.__scholarship

    @scholarship.setter
    def scholarship(self, new_s):
        if new_s < 1300:
            self.__scholarship = 1300
        elif new_s > 4000:
            self.__scholarship = 4000
        else:
            self.__scholarship = new_s

我有一个简单的问题:我需要计算特定组的奖学金总额和该组学生的中年。我在 def __init__ 中进行计算。但我也有 属性 和 setter 因条件而改变奖学金的数额。例如我们有 3 名学生:

student1 = Student(
    "Joe",
    "Hapfy",
    "Canes",
    datetime.date(1992, 3, 12),
    "Philosophy faculty",
    441,
    4300
)

student2 = Student(
    "Jane",
    "Mers",
    "Rodrigo",
    datetime.date(1998, 4, 29),
    "Historical faculty",
    441,
    2700
)

student3 = Student(
    "Pavlo",
    "Hornov",
    "Andriyovich",
    datetime.date(1997, 7, 22),
    "Mathematics faculty",
    171,
    1300
)

我想更改 student1 的奖学金。例如:

student1.scholarship = 1500
print(student1.scholarship)

但更改未保存,因为我在 dev __init__ 中进行计算。比如我输入的组数是441.

result = Student.overall_age/Student.amount_of_students
print("Total sum of scholarships: %d" % Student.summa)

奖学金总额为4300+2700,但由于setter4300将更改为4000,总额为6700。但现在我的student1奖学金为1500,我想收到结果1500+ 2700=4200。奖学金变更后我该如何进行此类计算?我应该在 dev __init__ 中使用方法或类似的方法而不是计算吗?

属性 setter 需要在必要时更新 Student.summa

由于setter需要读取旧值,在初始化内部__scholarship属性之前我们不能使用它。所以__init__()方法需要直接赋值给内部属性,而不是使用setter和self.scholarship.

from ClassPerson import Person


class Student(Person):
    number_of_group = eval(input("\nInput number of group: "))
    summa = 0
    amount_of_students = 0
    overall_age = 0

    def __init__(self, name, surname, patronymic, birthdate, faculty, group, scholarship):
        Person.__init__(self, name, surname, patronymic, birthdate)
        self.faculty = faculty
        self.group = group
        self.__scholarship = scholarship
        if Student.number_of_group == self.group:
            Student.summa += self.scholarship
            Student.overall_age += Student.age(self)
            Student.amount_of_students += 1

    @property
    def scholarship(self):
        return self.__scholarship

    @scholarship.setter
    def scholarship(self, new_s):
        old_s = self.__scholarship
        if new_s < 1300:
            self.__scholarship = 1300
        elif new_s > 4000:
            self.__scholarship = 4000
        else:
            self.__scholarship = new_s

        # Adjust Student.summa by the change in scholarship
        if self.group == Student.number_of_group:
            Student.summa += self.__scholarship - old_s