1 # class Chinese(object):
 2 class Chinese:
 3     '''
 4     类的说明性文档
 5     '''
 6     pass
 7 
 8 print(Chinese)  # <class '__main__.Chinese'>
 9 
10 # 类的实例化:类名+小括号,到底做了什么?
11 p1 = Chinese()
12 print(p1)       # <__main__.Chinese object at 0x0568C2B0>

类的属性操作

 1 class Chinese:
 2     country = 'China'
 3     def __init__(self,name):
 4         self.name = name
 5     def play(self):
 6         print("玩耍")
 7     def sing(self):
 8         print("唱歌")
 9 
10 print(Chinese)      # <class '__main__.Chinese'>
11 # 打印类的数据字典
12 print(Chinese.__dict__)
13 
14 #################——数据属性——######################
15 # 查看
16 print(Chinese.country)  # China
17 # 修改
18 Chinese.country = '中国'
19 print(Chinese.country)  # 中国
20 # 增加
21 Chinese.dang = ''
22 print(Chinese.dang)
23 # 删除
24 # print(Chinese.__dict__)
25 del Chinese.dang
26 # 打印类的数据字典
27 # print(Chinese.__dict__)
28 
29 #################——函数属性——######################
30 def eat(self):
31     print("吃吃吃")
32 def eat123(self):
33     print("吃吃吃123")
34 # 增加
35 Chinese.eat = eat
36 # 查看
37 print(Chinese.eat)      # <function eat at 0x04D0C8E8>
38 # 打印类的数据字典
39 # print(Chinese.__dict__)
40 # 修改
41 Chinese.eat = eat123
42 print(Chinese.eat)      # <function eat123 at 0x04DEC810>
43 # 删除
44 del Chinese.eat
45 # print(Chinese.__dict__)

实例的属性操作

 1 class Chinese:
 2     country = 'China'
 3     def __init__(self,name):
 4         self.name = name
 5     def play(self):
 6         print("玩耍")
 7     def sing(self):
 8         print("唱歌")
 9 
10 p1 = Chinese('yuan')
11 print(p1)           # <__main__.Chinese object at 0x05079CD0>
12 # 打印实例的数据字典【即init中的】
13 print(p1.__dict__)  # {'name': 'yuan'}
14 
15 #################——数据属性——######################
16 # 查看
17 print(p1.name)      # yuan
18 # 增加
19 p1.age = 18
20 print(p1.age)       # 18
21 # 打印实例的数据字典
22 print(p1.__dict__)  # {'name': 'yuan', 'age': 18}
23 
24 #不要修改底层的属性字典
25 # p1.__dict__['sex']='male'
26 # print(p1.__dict__)
27 # print(p1.sex)
28 
29 # 修改
30 p1.age = 23
31 print(p1.age)       # 23
32 print(p1.__dict__)  # {'name': 'yuan', 'age': 23}
33 # 删除
34 del p1.age
35 print(p1.__dict__)  # {'name': 'yuan'}
36 
37 #################——函数属性——######################
38 # 实例没有函数属性
39 # 类的属性修改会直接体现到实例
40 
41 def eat(self):
42     print("吃吃吃")
43 # 增加
44 p1.eat = eat
45 # 查看【忘记吧】
46 # p1.eat("###")    # 容易出错
47 # p1.eat(p1)

类与实例

 

 1 country = "中国****"
 2 class Chinese:
 3     country = 'China'
 4     def __init__(self,name):
 5         self.name = name
 6         print("--->", country)  # ---> 中国****
 7     def play(self):
 8         print("玩耍")
 9     def sing(self):
10         print("唱歌")
11 
12 p1 = Chinese('yuan')
13 # 打印实例的数据字典
14 print(p1.__dict__)  # {'name': 'yuan'}
15 # 【当实例的数据字典没有时】访问类的数据字典
16 print(p1.country)
17 
18 p1.country = "中国"
19 # 打印实例的数据字典
20 print(p1.__dict__)  # {'name': 'yuan', 'country': '中国'}
21 # 访问实例的数据字典
22 print(p1.country)   # 中国
23 
24 # 访问类的数据字典
25 print(Chinese.country)  # China
26 # 初始化时【country与p1.country、Chinese.country的区别】
27 p2 = Chinese('Lucy')    # 初始化时

 

posted on 2018-04-02 17:34  &&||!  阅读(122)  评论(0编辑  收藏  举报