【发布时间】:2021-12-30 23:10:13
【问题描述】:
class Person:
__name = ""
__contacts = []
__age = 0
__vaccination_state = False
__health_state = True
__quarantine_state = True
__symptom = "symptomatic"
def __init__(self, name, contacts, age, vaccination_state, health_state, quarantine_state, symptom):
self.__name = name
self.__age = age
self.__contacts = contacts
self.__vaccination_state = vaccination_state
self.__health_state = health_state
self.__quarantine_state = quarantine_state
self.__symptom = symptom
def GetName(self):
return self.__name
def GetAge(self):
return self.__age
def GetContacts(self):
return self.__contacts
def GetVaccination(self):
return self.__vaccination_state
def GetHealth(self):
return self.__health_state
def GetQuarantine(self):
return self.__quarantine_state
def GetSymptom(self):
return self.__symptom
def SetName(self, name):
self.__name = name
def SetAge(self, age):
self.__age = age
def SetAllContacts(self, contacts):
self.__contacts = contacts
def AddSingleContact(self, contact):
self.__contacts.append(contact)
def SetVaccination(self, vaccination_state):
self.__vaccination_state = vaccination_state
def SetHealth(self, healthState):
self.__health_state = healthState
self.CheckPositive()
def SetQuarantine(self, quarantine_state):
self.__quarantine_state = quarantine_state
def SetSymptom(self, symptom):
self.__symptom = symptom
def CheckPositive(self):
if self.__health_state == False:
self.__quarantine_state = True
start = datetime.datetime.now()
end = start + datetime.timedelta(10)
for i in range(len(self.__contacts)):
getattr(Person, self.GetContacts()[i]).SetQuarantine(True)
print(self.GetContacts()[i], " and his/her close contacts have been notified.\n", "Quarantine starts at", start, "and ends at", end)
for i in range(len(self.__contacts)):
getattr(Person, self.GetContacts()[i]).SetQuarantine(True)
A = Person("A", ["Z"], 17, True, True, False, "NA")
Z = Person("Z", ["A"], 17, True, True, False, "NA")
A.SetHealth(False)
SetQuarantine(True) 是我要调用的类中的一个函数。 self.GetContacts(i) 的返回值是一个列表。所以 self.GetContacts()[i] 是一个字符串,例如“name”。但我想获得一个属性,以便我可以调用函数 SetQuarantine(True)。此属性与字符串具有相同的值,但我不知道如何转换它。 谢谢!
【问题讨论】:
-
你能给我们
Person类的代码吗?就目前的情况而言,您没有给我们提供最低限度可重现的示例,这使我们更难诊断问题。 -
self.__contacts包含什么?与self.GetContacts返回的列表相同吗?还提供minimal reproducible example -
感谢您的 cmets!我刚刚编辑了代码,我将 A 的健康状态设置为 false,按照逻辑,Z 的隔离状态应该设置为 True。但由于它是一个字符串,它不能调用函数。你能帮我调用可以触发函数 SetQuarantine() 的函数吗?谢谢@BrokenBenchmark
-
@Matiiss 感谢您的评论。我在 BrokenBenchmark 的回复中回答了您的问题。