[英]Calculations in class Student
我創建了兩個類:不同模塊中的 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__
中進行計算。 但我也有財產和二傳手,可以根據條件改變獎學金的數額。 例如我們有 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,但由於 setter 4300 將更改為 4000,總額為 6700。但現在我的學生 1 獎學金為 1500,我希望收到結果 1500+2700=4200。 獎學金變更后我該如何進行此類計算? 我應該使用 method 或類似的東西而不是dev __init__
中的計算嗎?
屬性設置器需要在必要時更新Student.summa
。
由於 setter 需要讀取舊值,因此在初始化內部__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.