-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoop_objvar.py
More file actions
65 lines (51 loc) · 1.67 KB
/
Copy pathoop_objvar.py
File metadata and controls
65 lines (51 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#codding = UTF-8
class Robot:
"""表示有一个带有名字的机器人"""
population = 0
def __init__(self, name):
"""初始化数据"""
self.name = name
print("(Initializing {})".format(self.name))
# 当有人被创建时,机器人数量人口增加
Robot.population +=1
def __del__(self):
"""机器人挂了"""
print("{} is being destroyed!".format(self.name))
Robot.population -= 1
if Robot.population == 0:
print("{} was the last one.".format(self.name))
else:
print("There are the still {:d} robots working.".format(Robot.population))
def die(self):
"""机器人挂了"""
print("{} is being destroyed!".format(self.name))
Robot.population -=1
if Robot.population ==0:
print("{} was the last one.".format(self.name))
else:
print("There are the still {:d} robots working.".format(Robot.population))
def say_hi(self):
"""来自机器人的诚挚问候"""
print("Greetings,my masters call me {}.".format(self.name))
# @classmethod
# def how_manny(cls):
# """打印当前的机器人数量"""
# print("we have {:d} robots.".format(cls.population))
@staticmethod
def how_manny():
print("we have {:d} robots.".format(Robot.population))
droid1 = Robot("R1-D1")
droid1.say_hi()
Robot.how_manny()
droid2 = Robot("R2-D2")
droid2.say_hi()
Robot.how_manny()
print("\nRobots can do some work here.\n")
print("Robots have finished their work. So let's destroy them.")
droid1.die()
# 等价于
# del droid1
droid2.die()
# 等价于
# del droid2
Robot.how_manny()