类成员

类成员分为三大类:字段、方法、属性

python_面向对象

 注:所有成员中,只有普通字段的内容保存对象中,即:根据此类创建了多少对象,在内存中就有多少个普通字段。而其他的成员,则都是保存在类中,即:无论对象的多少,在内存中只创建一份。

 字段

  • 普通字段:属于对象
  • 静态字段:属于类
 1 #!/usr/bin/env python
 2 #coding=utf-8
 3 __author__ = 'yinjia'
 4 
 5 
 6 class Provice:
 7     #静态字段
 8     country = "中国"
 9 
10     def __init__(self,name):
11         temp = "xxx"
12         #普通字段,在对象中
13         self.name = name
14 
15 #访问普通字段
16 sz = Provice("深圳")
17 print(sz.sname)
18 
19 #访问静态字段
20 print(Provice.country)
21 
22 输出结果:
23 深圳
24 中国
方法定义使用

相关文章:

  • 2021-08-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-17
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-21
  • 2022-12-23
  • 2021-11-21
  • 2022-01-14
相关资源
相似解决方案