今日所学:
一、类和对象
1、类即种类、类别,是具有相同特征和技能,本身没有意义。即把一类事物的特征和技能整合到一起,是一个抽象概念
对象:对象是基于类创建的一个具体的事物,是具体存在的。
两者关系:类是一种数据结构,对象都是由类产生的。而由类产生对象的过程就叫做实例化。
2、例子:
# 学校类 # 数据属性:name、addr、type # 函数属性:test、zhao_sheng、build def school(name,addr,type): #定义一个学校的函数,方便把学校类的特征和动作整合到一起 def test(name): print("%s 正在举行考试" %name) return def zhao_sheng(name): print("%s 正在招生" %name) return def build(name): print("%s 正在扩张建设" %name) return school_1={"name":name, #用列表的形式定义一个类 "addr":addr, "type":type, "test":test, #在这里调用函数 "zhaosheng":zhao_sheng, "build":build } return school_1 s1=school("abc中学","xx市","公立学校") #由类创建出一个对象,以列表的形式返回 print(s1) print(s1["test"]("abc中学")) #调用对象的技能 print(s1["zhaosheng"]("abc中学")) print(s1["build"]("abc中学")) >>> {‘name‘: ‘abc中学‘, ‘addr‘: ‘xx市‘, ‘type‘: ‘公立学校‘, ‘test‘: <function school.<locals>.test at 0x00000207FF5CB4C0>, ‘zhaosheng‘: <function school.<locals>.zhao_sheng at 0x00000207FF5CB550>, ‘build‘: <function school.<locals>.build at 0x00000207FF5CB5E0>} abc中学 正在举行考试 abc中学 正在招生 abc中学 正在扩张建设
二、面向对象编程
1、如何声明类
#声明类 class 类名: “类的文档字符串” “类体” …… #声明函数 def 函数名(参数): “函数的文档字符串” “函数体” ……
2、属性
(1)数据属性:即变量
(2)函数属性:即函数,在面向对象中通常称为方法
类和对象都是用点来调用访问自己的属性。
3、实例类:
class School: def __init__(self, name, addr, type): #初始化函数,不能return值 self.name = name self.addr = addr self.type = type def test(self): print("%s 正在举行考试" %self.name) def zhao_sheng(self): print("%s 正在招生" %self.name) def build(self): print("%s 正在扩张建设" %self.name) print(School.__dict__) #查看类的属性,放在一个字典里(常用) print(dir(School)) #查看类的属性,但只有属性名,放在一个列表里 s1=School("abc中学","北京","公立学校") #定义了一个对象,实例化,相当于去调动__init__函数 # 即等于:s1=School.__init__(s1,name,addr,type) print(s1.__dict__) #可查看对象的属性 print(s1.name) #用.+属性名即可把字典中对应的值取出来。 >>> {‘__module__‘: ‘__main__‘, ‘__init__‘: <function School.__init__ at 0x000001F7BA38B4C0>, ‘test‘: <function School.test at 0x000001F7BA38B550>, ‘zhao_sheng‘: <function School.zhao_sheng at 0x000001F7BA38B5E0>, ‘build‘: <function School.build at 0x000001F7BA38B670>, ‘__dict__‘: <attribute ‘__dict__‘ of ‘School‘ objects>, ‘__weakref__‘: <attribute ‘__weakref__‘ of ‘School‘ objects>, ‘__doc__‘: None} [‘__class__‘, ‘__delattr__‘, ‘__dict__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__gt__‘, ‘__hash__‘, ‘__init__‘, ‘__init_subclass__‘, ‘__le__‘, ‘__lt__‘, ‘__module__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘__weakref__‘, ‘build‘, ‘test‘, ‘zhao_sheng‘] {‘name‘: ‘abc中学‘, ‘addr‘: ‘北京‘, ‘type‘: ‘公立学校‘} abc中学
3、注意
(1)实例只有数据属性,函数属性只要从类中引用即可。(即每一个实例仅保存自己的数据属性,要函数属性时从类中引用即可,省内存。)
(2)若实例中没有该属性,则会从该作用域往外找,去找类中的属性。
(3)self 就相当于实例本身,在实例调用类的函数属性时会自动将该实例传给self去执行函数。
(4)命名规范:类名首字母大写,函数属性:动词_名词
4、一些查看方法:
.__name__ 类的名字 .__doc__ 类的文档字符串 .__base__ 类的第一个父辈(所有的类都有一个共同的祖先:object) .__bases__ 类的所有父辈组成的元祖 .__dict__ 类的属性(字典形式) .__module__ 类所在的模块 .__class__ 实例所对应的类
三、增删改查
1、类的增删改查
class Chinese: country="China" def __init__(self,name): self.name=name def play_ball(self,ball): print("%s 正在打%s" %(self.name,ball)) def eat_food(self,food): print("%s 正在吃%s" %(self.name,food)) #查看 print(Chinese.country) print(Chinese.__dict__) p1=Chinese("happy") print(p1.__dict__) p1.play_ball("篮球") p1.eat_food("面包") print(p1.country) #p1这个实例本身没有country的属性,是往外找到类中去引用的。 #改 Chinese.country="中_国" p1=Chinese("banana") print(Chinese.country) print(p1.__dict__) #增加 Chinese.morale="the best" #直接增加一个属性 print(Chinese.morale) #在类中增加 print(p1.morale) #实例没有没有此属性,从内往外找类的属性,引用过来 #删除 del Chinese.morale #直接用del即可删除属性 print(Chinese.__dict__) >>> China {‘__module__‘: ‘__main__‘, ‘country‘: ‘China‘, ‘__init__‘: <function Chinese.__init__ at 0x00000195235DB4C0>, ‘play_ball‘: <function Chinese.play_ball at 0x00000195235DB550>, ‘eat_food‘: <function Chinese.eat_food at 0x00000195235DB5E0>, ‘__dict__‘: <attribute ‘__dict__‘ of ‘Chinese‘ objects>, ‘__weakref__‘: <attribute ‘__weakref__‘ of ‘Chinese‘ objects>, ‘__doc__‘: None} {‘name‘: ‘happy‘} happy 正在打篮球 happy 正在吃面包 China 中_国 {‘name‘: ‘banana‘} the best the best {‘__module__‘: ‘__main__‘, ‘country‘: ‘中国‘, ‘__init__‘: <function Chinese.__init__ at 0x00000195235DB4C0>, ‘play_ball‘: <function Chinese.play_ball at 0x00000195235DB550>, ‘eat_food‘: <function Chinese.eat_food at 0x00000195235DB5E0>, ‘__dict__‘: <attribute ‘__dict__‘ of ‘Chinese‘ objects>, ‘__weakref__‘: <attribute ‘__weakref__‘ of ‘Chinese‘ objects>, ‘__doc__‘: None}
2、对象的增删改查
class Chinese: country="China" def __init__(self,name): self.name=name def play_ball(self,ball): print("%s 正在打%s" %(self.name,ball)) def eat_food(self,food): print("%s 正在吃%s" %(self.name,food)) p1=Chinese("heaven") print(p1.__dict__) #查看 print(p1.name) p1.play_ball("basketball") p1.eat_food("bread") print(p1.play_ball) #查看函数属性的内存地址 #增加 #增加数据属性 p1.age=18 print(p1.__dict__) print(p1.age) #为实例增加函数属性 def test(self): #先自定义一个函数属性 print("这是来自实例的函数属性") p1.test=test #为实例的函数属性命名,增加此属性 print(p1.__dict__) p1.test(p1) #注意:实例调用类的函数属性时则不用穿参数,会自动把p1本身传给self,但若是调用实例自己地函数则没有自动传入,需自己加入参数 #改 p1.name="cany" p1.age=21 print(p1.__dict__) print(p1.name) #删除 del p1.age print(p1.__dict__) >>> {‘name‘: ‘heaven‘} heaven heaven 正在打basketball heaven 正在吃bread <bound method Chinese.play_ball of <__main__.Chinese object at 0x0000018E61DAAA60>> {‘name‘: ‘heaven‘, ‘age‘: 18} 18 {‘name‘: ‘heaven‘, ‘age‘: 18, ‘test‘: <function test at 0x0000018E61DEB310>} 这是来自实例的函数属性 {‘name‘: ‘cany‘, ‘age‘: 21, ‘test‘: <function test at 0x0000018E61DEB310>} cany {‘name‘: ‘cany‘, ‘test‘: <function test at 0x0000018E61DEB310>}
3、补充
class Chinese: country="China" def __init__(self,name): self.name=name def play_ball(self,ball): print("%s 正在打%s" %(self.name,ball)) def eat_food(self,food): print("%s 正在吃%s" %(self.name,food)) p1=Chinese("happy") p1.country="中_国" print(p1.country) #注意:在修改实例的属性时,并不会改到类中的属性 print(Chinese.country) >>> 中_国 China
country="China" class Chinese: def __init__(self,name): self.name=name print("--->",country) #注意:此处并没有用.的方式去调用属性,所以这的country仅是单纯的变量而已 def play_ball(self,ball): print("%s正在打%s" %(self.name,ball)) p1=Chinese("daary") >>> China
class Chinese: country = "China" l=["a","b"] def __init__(self,name): self.name=name #注意:此处并没有用.的方式去调用属性,所以这的country仅是单纯的变量而已 def play_ball(self,ball): print("%s正在打%s" %(self.name,ball)) p1=Chinese("happy") print(p1.l) p1.l=[1,22,444] print(p1.__dict__) #在改变实例的属性时,只会改到实例本身的,不会影响类的属性 print(Chinese.l) >>> [‘a‘, ‘b‘] {‘name‘: ‘happy‘, ‘l‘: [1, 22, 444]} [‘a‘, ‘b‘] p1.l.append("c") #这一步是引用类中的属性过来增加元素,所以改变的是类中的属性 print(p1.__dict__) print(Chinese.l) >>> {‘name‘: ‘happy‘} [‘a‘, ‘b‘, ‘c‘]
以上。
原文:https://www.cnblogs.com/211293dlam/p/12535865.html