类变量

class Person:

    country = "大清"

    def __init__(self, name, gender):
        self.name = name
        self.gender = gender


p = Person("武sir", "未知")
p2 = Person("太白", "两性")

Person.country = "中国"  # 类变量最好是使用类名来访问
# p.country = "大清" #  大坑::没有修改类变量
# p2.country = "大明"

print(p.country) # 类变量可以给对象使用
print(p2.country) # 类变量可以给对象使用

  

posted on 2018-12-19 19:37  =.===  阅读(179)  评论(0编辑  收藏  举报

导航