好的,这次我会尽量说清楚。
class Yes:
    def __init__(self):
        self.a=1
    def yes(self):
        if self.a==1:
            print "Yes"
        else:
            print "No, but yes"
class No(Yes):
    def no(self):
        if self.a==1:
            print "No"
        else:
            print "Yes, but no"
        self.a-=1 #Note this line
现在,在运行时:
Yes().yes()
No().no()
Yes().yes()
No().no()
我希望它打印出来:
Yes
No
No, but yes
Yes, but no
它给了我:
Yes
No
Yes
No
现在,我知道原因是因为我只是在 No 类中更改 Self.a 的值(还记得那行吗?)。我想知道是否有任何方法可以在 Yes 类中更改它,而仍在 No 类中(例如,如果我可以插入一些可以代替 self.a-=1 的东西)。