面向对象设计:
def dog(name,type,color): def voice(dog): print("%s can wangwangwang " %dog["name"] ) def run(dog): print("%s can run" %(dog["name"])) def show(dog): print("%s, %s, %s" %(dog["name"],dog["type"],dog["color"])) def init(name,type,color): dic={ "name":name, "type" :type, "color" :color, "voice" :voice, "run":run, "show":show } return dic res = init(name,type,color) return res dog1 = dog("a","zhong","red") dog1["voice"](dog1) dog1["show"](dog1)
面向对象编程
实例只有变量属性,没有函数属性
class People: '这是一个人类' def __init__(self,name,sex,age): print("初始化...") self.name = name self.sex = sex self.age = age def work(self): print("%s is working" %(self.name)) def eating(self): print("%s is eating food" %(self.name)) def show_self(self): print("my name is %s ,age is %d, sex is %s" %(self.name,self.age,self.sex)) p1 = People("alex","man",18) print(p1) # <__main__.People object at 0x000001839BD64E48> print(p1.__dict__) # {'sex': 'man', 'age': 18, 'name': 'alex'} print(People.__dict__) # {'__dict__': <attribute '__dict__' of 'People' objects>, '__module__': '__main__', # '__init__': <function People.__init__ at 0x000001921A677B70>, # '__doc__': None, 'eating': <function People.eating at 0x000001921ABF5C80>, # 'work': <function People.work at 0x000001921ABF5B70>, # 'show_self': <function People.show_self at 0x000001921ABF5BF8>, '__weakref__': <attribute '__weakref__' of 'People' objects>} print(p1.__dict__["name"]) # alex print(p1.name,p1.sex,p1.age) p1.show_self() People.eating(p1)
类的属性又称静态变量,或静态数据,这些数据与他们所属的类对象绑定,不依赖于任何实例
类的增删改查
class People: '这是一个人类' country="China" def __init__(self,name,sex,age): print("初始化...") self.name = name self.sex = sex self.age = age def work(self): print("%s is working" %(self.name)) def eating(self): print("%s is eating food" %(self.name)) def show_self(self): print("my name is %s ,age is %d, sex is %s" %(self.name,self.age,self.sex)) p1 = People("alex","man",18) print(p1) # <__main__.People object at 0x000001839BD64E48> print(p1.__dict__) # {'sex': 'man', 'age': 18, 'name': 'alex'} print(People.__dict__) # {'__dict__': <attribute '__dict__' of 'People' objects>, '__module__': '__main__', # '__init__': <function People.__init__ at 0x000001921A677B70>, # '__doc__': None, 'eating': <function People.eating at 0x000001921ABF5C80>, # 'work': <function People.work at 0x000001921ABF5B70>, # 'show_self': <function People.show_self at 0x000001921ABF5BF8>, '__weakref__': <attribute '__weakref__' of 'People' objects>} print(p1.__dict__["name"]) # alex print(p1.name,p1.sex,p1.age) p1.show_self() People.eating(p1) # 查看类的属性 print(People.country) # China print(p1.country) # China # 修改类的属性 People.country = "CHINA" print(People.country) # CHINA print(p1.country) # CHINA # 增加 # print(p1.color) # white 报错 People.color = 'white' print(People.color) # white print(p1.color) # white # 删除 del People.color # print(People.color) # white 报错
静态属性
@property
class Room: def __init__(self,name,width,length,height): self.Name = name self.Width = width self.Len = length self.Height = height @property def cal_volume(self): return self.Width * self.Len * self.Height r1 = Room('zhangshan',10,20,30) v = r1.cal_volume # 以属性的方式调用 print(v) # 6000 print(r1.__dict__) # {'Name': 'zhangshan', 'Width': 10, 'Len': 20, 'Height': 30}