<p>在考虑问题时,这可能是您想要考虑的一种方法,如果您想将改变重要性的能力与考试对象结合起来:</p>
<pre><code>class Exam:
# COMMENT: set a default importance of False...
def __init__(self, day, importance=False):
self.day = day
self.importance = importance
# COMMENT: since you provide the day when the object is created,
# use that value (ie, self.day) in the input function call
# and overwrite the existing self.importance value.
def is_important(self):
answer = input("Ìs " + self.day + " Important?")
if answer == "True":
self.importance = True
elif answer == "False":
self.importance = False
# COMMENT: When you call the Class, you can provide it with the day
# and immediately provide it with an importance level.
mon = Exam("Monday", True)
tue = Exam("Tuesday", False)
# COMMENT: but you also have the flexibility to call the class and accept
# the default importance OR overwrite it later by calling the
# is_important() method
wed = Exam("Wednesday")
wed.is_important()
print(mon.importance)
print(tue.importance)
print(wed.importance)
</code></pre>