这是代码。
class average_person:
def __init__(self, first_name, last_name, height, weight, email):
self.height = height
self.weight = weight
self.first_name = first_name
self.last_name = last_name
self.email = email
self.email = self.first_name + '.' + self.last_name + email
def doc(self):
print(f"Name: {self.first_name + ' ' + self.last_name} Height: {self.height} Weight: {self.weight}, Email: {self.email}")
def mail(self):
print(f"Email: {self.email}")
def change_email(self, gmail):
self.email = self.first_name + '.' + self.last_name + gmail
print(self.email)
# Create 5 instances of the class
person_1 = average_person("Abu Bakr", "Hassan", "5'6", "122 lbs", '@gmail.com')
person_2 = average_person("Syed", "Jameel", "5'2", "99 lbs", '@cpsaonline.org')
person_3 = average_person("Abdur-Rahman", "Mohammed", "4'10", "100 lbs", '@gmail.com')
person_4 = average_person("Haroon", "Abdul-Mannan", "4'11", "98 lbs", '@company.org')
person_5 = average_person("Bilal", "Ala", "5'1", "101 lbs", '@cpsa.school')
# Running the functions
代码运行良好。我只是不知道如何使用影响类的所有实例的一个单独的命令来更改实例的特定属性或变量(我想更改电子邮件)。
起初我想也许我可以使用@classmethods,但我不知道我在做什么,所以这真的没有任何进展。我只需要知道该怎么做以及解释它为什么有效。
嗯,我认为这是不可能的,因为每个实例都是孤立的。尝试使用
for
和 eval
来代替。