类属性与对象属性的增删改查
类属性的增删改查
class School:
"""
文档
"""
Teacher = "老王"
def __init__(self,name):
self.name = name
def Examination(self):
print("%s的班级正在考试"%self)
p1 = School("小李")
'''类数据属性的增删改查
查
print(School.Teacher)
改
School.Teacher="老李"
print(School.Teacher)
增
School.foo ="小孙"
print(School.__dict__)
删
del School.foo
print(School.__dict__)
'''
#类函数属性的增删改查同实例属性
def eat_food(self,food):
print("正在吃")
School.eat = eat_food
print(School.__dict__)
School.eat
对象(实例)属性的增删改查
class Chinese:
country = "china"
def __init__(self,name):
self.mingzi = name
def play_ball(self,ball):
print("%s 正在打 %s"%(self.mingzi,ball))
p1 = Chinese("alex")
print(p1.__dict__)
#查
print(p1.mingzi)
p1.play_ball("篮球")#实际上是调用类的函数属性
#增
p1.age = 18
print(p1.__dict__)
#改
p1.age = 29
print(p1.__dict__)
#删
del p1.age
print(p1.__dict__)
小结
注意:点的方式调用要么跟类有关,要么跟实例有关,不加点调用就是普通变量
#点的方式调用要么跟类有关,要么跟实例有关,不加点调用就是普通变量
country = "中国"
class Chinese:
country = "china"
l = ["a","b"]
def __init__(self,name):
self.mingzi = name
def play_ball(self,ball):
print("%s 正在打 %s"%(self.mingzi,ball))
p1 = Chinese("alex")
p1.country = "老王" #属于给实例化新增一个字典,不影响类的字典
print(Chinese.country)
print(p1.country)
p1.l.append("c") #属于调用类的属性,与实例化无关
print(Chinese.l)
print(p1.l)
print(p1.__dict__)#实例化字典中没有l