初学者的问题:
有人可以向我解释为什么change_pos
方法在以下代码中不起作用,并且没有将坐姿从“真”更改为“假”(或者相反)吗? p>
如何在一行代码中对整个实例change_pos
实施此(p1, p2)
方法?
class Person:
def __init__(self, name, Personality, isSitting):
self.name=name
self.Personality = Personality
self.isSitting=isSitting
def sit_down(self):
self.isSitting=True
def stand_up(self):
self.isSitting=False
def change_pos(self):
if (self.isSitting==True):
self.isSitting==False
else:
self.isSitting==True
p1=Person("Alice", "aggressive", False)
p2=Person("Becky", "talkative", True)
print (p1.name,'is', p1.isSitting ,'about sitting')
print (p2.name,'is', p2.isSitting ,'about sitting')
p2.change_pos()
print (p1.name,'is', p1.isSitting ,'about sitting')
print (p2.name,'is', p2.isSitting ,'about sitting')
输出为:
Alice is False about sitting
Becky is True about sitting
Alice is False about sitting
Becky is True about sitting