面向对象编程 class
1.类
一个种类,一个模型,类在用的时候必须实例化
类分两种,经典类和新式类
经典类:首字母大写
class Person: #经典类 #类名首字母大写
pass
新式类:
class Person(object):#新式类 pass
1 class Person: #经典类 #类名首字母大写 链接mysql的时候可以写在构造函数里 2 def __init__(self,name):#构造函数,self类里面相互调用 3 self.name = name #name不能写死 4 self.nose = 1 #鼻子 属性 5 self.face = 1 #脸 6 self.head = 10#脑子 7 self.wing = 2 #翅膀 8 print('开始造人。。') 9 def driver(self): 10 print('老司机,开车非常稳') 11 self.eat() # 调用类里面的函数 12 def fly(self): 13 print('%s在飞。。'%self.name) 16 def eat(self): 17 print('%s吃火锅,吃小龙虾,喝啤酒'%self.name)
类在调用的时候必须实例化,如果直接调用会报错
fanxx = Person(name = 'zhang') 类名加()就是实例化
构造函数__init__(self)
构造函数就是类在实例化的时候执行的函数
构造函数是类在实例化的时候,会自动执行他
if __name__ ==__main__
这句话一般是做测试或者调试时用的,判断是直接执行还是在别的地方导入
1.如果是直接运行这个python文件时,一点用都没有
2.作为模块导入时这条代码之下的代码都不会被执行
2.对象
指具体的东西,根据模型造出来的东西
3.实例
实例和对象是一样的
4.实例化
实例化就是造东西的过程
5.属性
变量
6.方法
就是类里面的函数
7.继承
继承就是把父类的名字写在括号里
父类和子类都有的方法,会重写父类的方法
class Lily(object): def __init__(self): self.money = 100000 self.house = '3环里20套' def sing(self): print('唱歌') def dance(self): print('跳广场舞') def lm(self): print('找朋友') class lucy(Lily): #继承就是把父类的名字写在括号里 def dance(self): print('跳霹雳舞') #父类和子类都有的方法,会重写父类的方法 aa = lucy() print(aa.money) aa.dance() aa.lm()
私有:比如链接redis, 如果有人把端口号或者配置什么的修改了,那自己就登不上了,所以这个时候就用到了私有
def __close(self):#私有方法 print('close')
例子:
1 import redis 2 class My(object): 3 def __init__(self): 4 self.__host = '118.24.3.40' 5 self.__port=6379 6 self.__password='HK139bc&*' 7 8 self.r = redis.Redis(host=self.host,port=self.port,password=self.password) 9 def get(self,k): 10 res = self.r.get(k) 11 if res: 12 return res.decode() 13 return None 14 def __close(self):#私有方法 15 print('close') 16 def str_set(self,k,v): 17 self.r.set(k,v) 18 def str_del(self,k):#str类型删除key 19 self.r.delete(k) 20 def hash_get(self,large_k,small_k): 21 self.r.hget(large_k,small_k).decode() 22 def hash_getall(self,large_k): 23 self.r.hgetall(large_k) 24 def hash_del(self,large_k,small_k): 25 self.r.hdel(large_k,small_k)#删除指定key 26 def clean_redis(self,large_k):#清理redis 27 self.r.delete(large_k)
调用:
1 my = My() 2 my.port = 9999#如果别人改port就连不上redis,这个时候不想让别人改,就要用到私有 3 print(my.__host)#在变量前加__代表私有,出了类就不能用了 4 # 运行后 5 # Traceback (most recent call last): 6 # File "G:/catherine/python/day9/private.py", line 27, in <module> 7 # my = My() 8 # File "G:/catherine/python/day9/private.py", line 8, in __init__ 9 # self.r = redis.Redis(host=self.host,port=self.port,password=self.password) 10 # AttributeError: 'My' object has no attribute 'host'
1 class Baby(): 2 country = 'China'#类变量,公共变量,每一个实例都可以用 3 # def __init__(self,name): 4 # self.name = name 5 # self.money = 5000 6 # self.sex = '女' 7 def my(self): 8 self.name = 'nn' 9 def cry(self): 10 print('wuwu....') 11 @property#加上property, 调用时,不用加(),如果没有@property,调用的时候加() 12 def hhh(self): 13 return 198 14 @classmethod 15 def xm(cls):#cls代表的是Baby 16 print(cls.country) 17 print('我是类方法') 18 print(cls.name)#能点出来但是不能print 19 20 @staticmethod 21 def xh(): 22 print('这个是静态方法,它和一个没写在类里面的函数一样') 23 #静态方法就是一个普通函数,只不过是写在类里面而已,它用不了类变量,类方法,实例变量,实例方法 24 # Baby.xm() 25 #运行后 26 # Traceback (most recent call last): 27 # China 28 # File "G:/catherine/python/day9/fanxx.py", line 19, in <module> 29 # 我是类方法 30 # Baby.xm() 31 # File "G:/catherine/python/day9/fanxx.py", line 18, in xm 32 # print(cls.name)#能点出来但是不能print 33 # AttributeError: type object 'Baby' has no attribute 'name' 34 35 # 1.不实例化,直接用类名调用xm方法 36 # Baby.xm() 37 #运行后 38 # China 39 # 我是类方法 40 #2.实例化之后,再通过实例化之后的对象调用xm方法 41 # kl = Baby() 42 # kl.xm() 43 #运行后 44 # China 45 # 我是类方法 46 # Baby.country='USA'#类变量修改会影响所有的实例,类变量可以直接通过类名.xx直接修改 47 # ll = Baby() 48 # 49 # # ll.country='Japan' 50 # 51 # print(ll.country) 52 # lu = Baby() 53 # print(lu.country) 54 55 # b = Baby() 56 # b.hhh() 57 # print(b.hhh) 58 # hh = Baby('lily')#实例化这个类 59 # print(hh.name) 60 # aa = Baby('lucy') 61 # print('lily',id(hh)) 62 # print(aa.name) 63 # print(hh.money) 64 # hh.car = 'bmw' 65 # print(hh.car) 66 # Baby.cry() #加self的是实例方法,必须实例化后才能调用 67 #运行后 68 #Traceback (most recent call last): 69 # File "G:/catherine/python/day9/fanxx.py", line 23, in <module> 70 # Baby.cry() 71 # TypeError: cry() missing 1 required positional argument: 'self'