[英]Calculations in class Student
I have created two classes: Person and Student in different modules.我创建了两个类:不同模块中的 Person 和 Student。 Here is my class Person:这是我的 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
Here is my class Student:这是我的 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
I have one simple problem: I need to calculate for specific group overall sum of scholarships and middle age of students of this group.我有一个简单的问题:我需要计算特定组的奖学金总额和该组学生的中年。 I do calculations in def __init__
.我在def __init__
中进行计算。 But i also had property and setter to change the amount of scholarship due to conditions.但我也有财产和二传手,可以根据条件改变奖学金的数额。 So for example we have 3 students:例如我们有 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
)
I want to change student1 scholarship.我想更改 student1 奖学金。 For example:例如:
student1.scholarship = 1500
print(student1.scholarship)
But the changes are not saved, cause i do calculations in dev __init__
.但是更改没有保存,因为我在dev __init__
中进行了计算。 For example, I input number of group as 441.比如我输入组号为441。
result = Student.overall_age/Student.amount_of_students
print("Total sum of scholarships: %d" % Student.summa)
The sum of scholarships will be 4300+2700, but due to setter 4300 will be changed to 4000 and sum will be 6700. But now my student1 scholarship is 1500 and i want to receive result 1500+2700=4200.奖学金总额为 4300+2700,但由于 setter 4300 将更改为 4000,总额为 6700。但现在我的学生 1 奖学金为 1500,我希望收到结果 1500+2700=4200。 How can i do such calculations after changes of scholarship?奖学金变更后我该如何进行此类计算? Should I use method or something like that instead of calculations in dev __init__
?我应该使用 method 或类似的东西而不是dev __init__
中的计算吗?
The property setter needs to update Student.summa
when necessary.属性设置器需要在必要时更新Student.summa
。
Since the setter needs to read the old value, we can't use it before we initialize the internal __scholarship
attribute.由于 setter 需要读取旧值,因此在初始化内部__scholarship
属性之前我们不能使用它。 So the __init__()
method needs to assign directly to the internal attribute, rather than using the setter with self.scholarship
.所以__init__()
方法需要直接赋值给内部属性,而不是使用self.scholarship
的 setter。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.