Python面向对象(类的成员之字段)
day24
类的成员之字段
# 字段
- 普通字段,保存在对象中,执行只能通过对象访问
- 静态字段,保存在类中, 执行 可以通过对象访问 也可以通过类访问
1 class Province: 2 #静态字段,属于类 3 country = '中国' 4 5 def __init__(self, name): 6 #普通字段,属于对象 7 self.name = name 8 9 henan = Province('河南') 10 henan.name = '豫'#普通字段,属于对象,通过对象访问 11 12 print(henan.country)#静态字段,通过对象访问 13 print(Province.country)#静态字段,通过类访问
执行结果:
中国
中国
Process finished with exit code 0