点此进入
一段Python代码,可以用来模拟含羞草的“害羞”行为:
class Mimosa: |
|
def __init__(self): |
|
self.shy = True |
|
def feel_shy(self): |
|
if self.shy: |
|
print("The mimosa is feeling shy!") |
|
else: |
|
print("The mimosa is feeling brave!") |
|
def bruise(self): |
|
self.shy = True |
|
print("The mimosa has been bruised!") |
|
def recover(self): |
|
self.shy = False |
|
print("The mimosa has recovered!") |
这个类有四个方法:
__init__()
:初始化含羞草的“害羞”状态为 True
。feel_shy()
:打印出含羞草当前的“害羞”状态。bruise()
:模拟含羞草受到外界刺激,使其进入“害羞”状态。recover()
:模拟含羞草逐渐恢复,不再“害羞”。