我在不同的模塊中創建了兩個類:Person 和 Student。這是我的班級人:
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
這是我的班級學生:
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。但是現在我的 student1 獎學金是 1500,我想收到結果 1500 2700=4200。獎學金變更后如何計算?我應該使用方法或類似的東西而不是計算dev __init__嗎?
uj5u.com熱心網友回復:
必要時需要更新屬性設定器Student.summa。
__scholarship由于setter需要讀取舊值,所以在初始化內部屬性之前我們不能使用它。所以__init__()方法需要直接賦值給內部屬性,而不是使用setter with 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/464916.html
上一篇:具有不同引數的Python類繼承
下一篇:非共享方法的虛函式
