Python-操作方法

时间:2018-07-14 09:05:32

标签: python class methods

初学者的问题:

  1. 有人可以向我解释为什么change_pos方法在以下代码中不起作用,并且没有将坐姿从“真”更改为“假”(或者相反)吗? p>

  2. 如何在一行代码中对整个实例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

0 个答案:

没有答案