标签:传递 进化 http 项目 定义 ict 思维 chinese sep
1 # 面向对象设计 2 def dog(name,gender,type): # 相当于类的概念 3 # 动作 4 def jiao(dog): 5 print("一条 %s 正在叫" %dog["name"]) 6 def chi(dog): 7 print("一条 %s 正在吃骨头"%dog["name"]) 8 # 属性 特征 9 def init(name,gender,type): # 初始化函数 10 dog1 = { 11 "name": name, 12 "gender": gender, 13 "type": type, 14 "jiao": jiao , 15 "chi": chi 16 } 17 return dog1 18 return init(name,gender,type) 19 d1 = dog("xiaoming","mu","zangao") # 相当于一个对象 20 d2 = dog("xiaogang","mu","zangao") 21 print(d1) 22 d1["chi"](d1) 23 d2["jiao"](d2)
class Teacher: ‘这是一个教师类’ def __init__(self,name,age,job_in,teach_subjects): # 实例化 self.name = name # self 实例本身 self.age = age # 相当于teacher1.name = name self.job_in = job_in self.teach_subjects = teach_subjects def teach(self): print("%s 在 %s 教 %s" %(self.name,self.job_in,self.teach_subjects)) def talk(self,name,content): print("%s 在和 %s 讨论 %s"%(self.name,name,content)) teacher1 = Teacher("lhf",33,"oldboy","python") # 对象# 就相当于,理论上teacher1 = Teacher.__init__(teacher1,name,age,job_in,teach_subjects) teacher2 = Teacher("wusir",28,"oldboy","python") # 对象 teacher1.teach() # 调用教学方法 teacher2.talk("小明","如何学好python")
5.1 属性
类是用来描述一类事物,类的对象指的是这一类事物中的一个个体
是事物就要有属性,属性分为
1:数据属性:就是变量
2:函数属性:就是函数,在面向对象里通常称为方法
注意:类和对象均用点来访问自己的属性
类的数据属性
1 class ChinesePeople: 2 government=‘共产_党‘ 3 4 print(ChinesePeople.government)
类的函数属性
1 class Chinese: 2 ‘这是一个中国人的类‘ 3 goverment = "共产_党" # 数据属性 4 def __init__(self,name,age,gender): # 创建一个实例对象 5 print(" 我开始了") 6 self.mingzi = name 7 self.nianling = age 8 self.xingbei = gender 9 def gongfu(): # 函数属性 又称方法 10 print("会功夫") 11 def xihuanchi(self): 12 print("%s 讲究吃"%self.mingzi) 13 p1 = Chinese("小明",18,"female") # 对象 14 print(p1.__dict__) # 相当于init的返回值 执行__init__函数 {‘mingzi‘: ‘小明‘, ‘xingbei‘: ‘female‘, ‘nianling‘: 18} 15 print(p1.mingzi) # 小明 16 print(p1.goverment) #共产_党 17 print(Chinese.__dict__)# {‘__init__‘: <function Chinese.__init__ at 0x0000016DDEDCF378>, ‘__dict__‘: <attribute ‘__dict__‘ of ‘Chinese‘ objects>, ‘gongfu‘: <function Chinese.gongfu at 0x0000016DDEDCF400>, ‘goverment‘: ‘共产_党‘, ‘xihuanchi‘: <function Chinese.xihuanchi at 0x0000016DDEDCF488>, ‘__module__‘: ‘__main__‘, ‘__doc__‘: ‘这是一个中国人的类‘, ‘__weakref__‘: <attribute ‘__weakref__‘ of ‘Chinese‘ objects>} 18 Chinese.gongfu() #会功夫 19 Chinese.xihuanchi(p1) # 小明 讲究吃 20 p1.xihuanchi() #小明 讲究吃
查看类属性
我们定义的类的属性到底存到哪里了?有两种方式查看
dir(类名):查出的是一个名字列表
类名.__dict__:查出的是一个字典,key为属性名,value为属性值
特殊的类属性
1 # class dog : 2 # ‘这是定义一个狗的对象‘ 3 # name = "藏獒" 4 # def jiao(self): 5 # print("一条 %s 正在汪汪汪" %self) 6 # dog.jiao("藏獒") 7 # print(dir(dog)) # 显示结果是一个列表,包含类(包含内建属性在内的)所有的属性名 8 # print(dog.__dict__) # 显示结果是一个字典,包含类的所有属性:属性值 9 # 10 # print(dog.__name__)# 类C的名字(字符串) 11 # print(dog.__doc__)# 类C的文档字符串 12 # print(dog.__base__)# 类C的第一个父类(在讲继承时会讲) 13 # print(dog.__bases__)# 类C的所有父类构成的元组(在讲继承时会讲) 14 # print(dog.__dict__)# 类C的属性 15 # print(dog.__module__)# 类C定义所在的模块 16 # print(dog.__class__)# 实例C对应的类(仅新式类中)
# 类属性的增删改查 # class Chinese: # country=‘China‘ # def __init__(self,name): # self.name=name # # def play_ball(self,ball): # print(‘%s 正在打 %s‘ %self.name,ball) # # def say_word(self,word): # # print(‘%s 说 %s‘ %(self.name,word)) # 数据属性的增删改查 # 查看类的属性 # print(Chinese.country) # 修改 # Chinese.country = "English" # print(Chinese.country) # 增加 # Chinese.location = "Asia" # print(Chinese.__dict__) # print(Chinese.location) # # 删除 # del Chinese.location # print(Chinese.__dict__) # 实例属性的增删改查 # 查看 # p1 = Chinese("xiaoming") # print(p1.__dict__) # # 查看 # print(p1.name) # # 删除 # del p1.name # print(p1.__dict__) # # 增加 # p1.gender = "man" # print(p1.__dict__) # # 修改 # p1.gender = "woman" # print(p1.__dict__) # 函数属性的增加修改 # class Chinese: # country=‘China‘ # def __init__(self,name): # self.name=name # # def play_ball(self,ball): # print(‘%s 正在打 %s‘ %(self.name,ball)) # # def say_word(self,word): # # print(‘%s 说 %s‘ %(self.name,word)) # # 增加 # def eat_food(self,food): # print("%s 正在吃 %s" %(self.name,food)) # Chinese.eat = eat_food # print(Chinese.__dict__) # p1 = Chinese("xiaoming") # # print(p1.__dict__) # p1.eat("fan")
# 注意事项
1 # 1 2 # class Chinese: 3 # country = "China" 4 # def __init__(self,name): 5 # self.name = name 6 # def play_ball(self,ball): 7 # print("%s 正在打 %s" %(self.name,ball)) 8 # p1 = Chinese(‘alex‘) 9 # print(p1.country) 10 # p1.country = "日本" # 相当于在实例里面创建了个新的 11 # print(p1.__dict__) # {‘name‘: ‘alex‘, ‘country‘: ‘日本‘} 12 # print("类的——",Chinese.country) 13 # print("实例的",p1.country) 14 15 # 2、不调用 . 直接找全局变量 16 # country = "中国" 17 # class Chinese: 18 # country = "China" 19 # def __init__(self,name): 20 # self.name = name 21 # print(country) # 中国 22 # def play_ball(self,ball): 23 # print("%s 正在打 %s" %(self.name,ball)) 24 # p1 = Chinese(‘alex‘) 25 # print(p1.country) # China
Python之路【第六篇】python基础 之面向对象(一)
标签:传递 进化 http 项目 定义 ict 思维 chinese sep
原文地址:http://www.cnblogs.com/sunkai1993/p/6203551.html