类与对象

一、类属性增删改查

 1 class People:
 2     country = 'china'
 3     def __init__(self,name,action):
 4         self.name = name
 5         self.action = action
 6 
 7     def eat_food(self,food):
 8         print('%s 正在吃 %s'%(self.name,food))
 9 #查看类数据
10 print(People.country)#china
11 #增加类数据
12 p1 = People('zhuo','action')
13 People.age = 14
14 print(People.__dict__) #字典形式查看类数据,包括方法,属性
15 print(p1.age) #14
16 #删类数据
17 del People.age
18 print(People.__dict__)
19 #改类数据
20 People.country = 'chin'
21 print(People.country) #chin
22 
23 #增加类中方法
24 def run(self):
25     print('running')
26 People.run = run
27 print(People.__dict__)
28 p1.run() #running
29 
30 #修改类中方法
31 People.eat_food = run
32 p1.eat_food() #running

 

posted on 2018-03-09 13:14  卓某  阅读(121)  评论(0编辑  收藏  举报

导航