面向对象:指挥者
类:一个模板
对象和实例:就是根据这个模板造出来的东西
实例化:把模型做成实例的过程
属性:类里面的变量
方法:类里面的函数
构造函数:类在初始化的时候做的一些操作,实例化是会自动执行
析构函数 :函数被销毁时执行的
self:代表的就是本类对象
实例变量:必须实例化之后才能用的变量
类变量:不需要实例化就可以用,定义在类里面定义的,是公用的
属性方法:看着像变量的方法
私有变量/方法:在变量/函数 前面加__ ,只能在类里面被调用,出了类就不能用了
类方法:类里面定义的公共方法,不需要实例化就可以调用
什么时候用类方法:
这个方法里面没有用到实例变量或者没有调用实例方法,只用到了类变量时
实例方法:实例方法必须实例化之后才可以使用 实例方法里面可以随便调用类方法、类变量
静态方法:只是定义在类里面的一个普通函数而已
什么时候用静态方法:
如果这个函数没有用到类变量、类方法、实例变量、实例方法,就可以把它定义成静态方法
类方法、实例方法 可以随便调用静态方法
class People: #定义类:类名第一个字母大写 经典类 # class People(object): #新式类 eye = 2 #属性 mouth = 1 shengao = 180 money = 1000000 def __init__(self, name): # 构造函数 self.name = name print('造了一个人,这个人是%s' % name) def cry(self): #方法 print('哭') def makeMoney(self): #方法 print(id(self)) print('%s 挣了20w' %self.name) niuniu = People('牛牛') #实例化一个对象,niuniu; 构造方法中必须传入一个name参数 ,不传会报错 print('牛牛的内存地址',id(niuniu)) #niuniu的内存地址和self的内存地址是一样的,说明类中的self就是代表当前实例 niuniu.makeMoney() # 简化写法,调用类中的方法 makemaney(self),self是必填参数,调用的时候python自动把niuniu这个对象传给了makemaney() People.makeMoney(niuniu) #原始写法,将niuniu传给makeMoney()
oop_mysql.py
import pymysql class MySql: # 初始化的时候创建一个数据库连接,和一个游标 def __init__(self,host,user,password,db,port=3306,charset='utf8'): self.conn = pymysql.connect(host = host,user = user,password = password,db = db,port = port,charset = charset) self.cur =self.conn.cursor(cursor=pymysql.cursors.DictCursor) def excute_many(self,sql): #从数据库中返回所有数据 self.cur.execute(sql) return self.cur.fetchall() def excute_one(self,sql): #从数据库中返回一条数据 self.cur.execute(sql) return self.cur.fetchone() def __del__(self): #析构函数(在实例被销毁的时候自动执行) 关闭连接 self.cur.close() self.conn.close() db = MySql('118.24.3.40','jxz','123456','jxz') res = db.excute_one('select * from app_myuser where username="hahaha";') print(res) res = db.excute_many('select * from app_myuser limit 20;') print(res)
类变量、实例变量、属性方法
class People: country = 'China' #类变量 不需要实例化就能用 def __init__(self,name,sex): self.name = name #实例变量 self.sex = sex def say(self): print('name '+self.name) print('sex '+self.sex) print('country '+self.country) @property #属性方法 def get_name(self): #将一个函数变成了一个变量来用,变量的值就是函数的返回值,函数没有入参时可以这样用 return self.name print(People.country) #类变量,不用实例化就可以使用 xiaojun = People('xiaojun','male') xiaojun.say() print(xiaojun.get_name)
继承
class Student: def sql(self): print('sql') def linux(self): print('linux') class PyStudent(Student): def python(self): print('python') class XnStudent(Student): def xn(self): print('xn') def sql(self): #重写父类方法 print('mysql') py = PyStudent() py.linux() #linux py.sql() #sql py.python() #python xn = XnStudent() xn.linux() #linux xn.sql() #mysql
子类中通过super()调用父类中的方法
class Lw: def about_me(self,name): print('[%s]会抽烟喝酒烫头'%name) class Xw(Lw): def about_me(self,name,age): super().about_me(name) #super指的就是父类 super()--实例化父类 print('年龄是', age) #子类中加了一个方法 wxm = Xw() wxm.about_me('王小明',18)
私有变量、私有方法、类变量、类方法
class DB: port = 3306 #类变量 def __init__(self,host,user,password,db): self.host = host #实例变量、成员变量 self.__user = user #私有变量(前面加__) 出了类就不能用了 self.__password = password self.db = db def sql(self,sql): #实例方法 加了self的都是实例方法(self告诉你是哪个实例调用的该方法),不初始化实例是不能调用的 print('执行sql') @classmethod #类方法 直接用类名调用,不需实例化 def help(cls): #当方法里面没有用到实例变量或者没有调用实例方法的时候,只用到了类变量时就可以定义成一个类方法 print('cls的内存地址',id(cls)) #id(cls) 和 id(DB)是一样的 cls代表的就是DB这个类 def __help(self): #私有函数 只能在类里面用 print(self.host) print(self.__user) #类里面可以调用私有变量 print(self.__password) print(self.db) def get_port(self): print(self.port) d = DB('127.0.0.1','lh','123456','mine') d.sql('sql') print(d.__user) # 报错 AttributeError: 'DB' object has no attribute '__user' d.__help() #报错 AttributeError: 'DB' object has no attribute '__help' mysql = DB() mysql.get_port() #3306 mysql.port = 3307 #给mysql实例加了一个port=3307 并没有改变类变量 DB.port=3306 mysql.get_port() #3307 print(DB.port) #3306 oracle = DB() oracle.get_port() # 3306 oracle实例没有加port ,所以还是等于类变量port = 3306 DB.port=3308 #修改类变量 # 类名.类变量 = XX 修改类变量的值 # self.属性 =XX 实例名.属性 =XX 修改的都是实例属性的值,不影响类变量 DB.get_port() #不能调用 加了self的方法,不初始化实例是不能调用的 DB.help() #cls的内存地址 3180534563992 print(id(DB)) #3180534563992
静态方法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import turtle as t import time class Pig: words = '爱你呦' #类变量 @classmethod #类方法 def peiQi(cls): t.pensize(4) t.hideturtle() t.colormode(255) t.color((255, 155, 192), "pink") t.setup(840, 500) t.speed(0.5) # 鼻子 t.pu() t.goto(-100, 100) t.pd() t.seth(-30) t.begin_fill() a = 0.4 for i in range(120): if 0 <= i < 30 or 60 <= i < 90: a = a + 0.08 t.lt(3) # 向左转3度 t.fd(a) # 向前走a的步长 else: a = a - 0.08 t.lt(3) t.fd(a) t.end_fill() t.pu() t.seth(90) t.fd(25) t.seth(0) t.fd(10) t.pd() t.pencolor(255, 155, 192) t.seth(10) t.begin_fill() t.circle(5) t.color(160, 82, 45) t.end_fill() t.pu() t.seth(0) t.fd(20) t.pd() t.pencolor(255, 155, 192) t.seth(10) t.begin_fill() t.circle(5) t.color(160, 82, 45) t.end_fill() # 头 t.color((255, 155, 192), "pink") t.pu() t.seth(90) t.fd(41) t.seth(0) t.fd(0) t.pd() t.begin_fill() t.seth(180) t.circle(300, -30) t.circle(100, -60) t.circle(80, -100) t.circle(150, -20) t.circle(60, -95) t.seth(161) t.circle(-300, 15) t.pu() t.goto(-100, 100) t.pd() t.seth(-30) a = 0.4 for i in range(60): if 0 <= i < 30 or 60 <= i < 90: a = a + 0.08 t.lt(3) # 向左转3度 t.fd(a) # 向前走a的步长 else: a = a - 0.08 t.lt(3) t.fd(a) t.end_fill() # 耳朵 t.color((255, 155, 192), "pink") t.pu() t.seth(90) t.fd(-7) t.seth(0) t.fd(70) t.pd() t.begin_fill() t.seth(100) t.circle(-50, 50) t.circle(-10, 120) t.circle(-50, 54) t.end_fill() t.pu() t.seth(90) t.fd(-12) t.seth(0) t.fd(30) t.pd() t.begin_fill() t.seth(100) t.circle(-50, 50) t.circle(-10, 120) t.circle(-50, 56) t.end_fill() # 眼睛 t.color((255, 155, 192), "white") t.pu() t.seth(90) t.fd(-20) t.seth(0) t.fd(-95) t.pd() t.begin_fill() t.circle(15) t.end_fill() t.color("black") t.pu() t.seth(90) t.fd(12) t.seth(0) t.fd(-3) t.pd() t.begin_fill() t.circle(3) t.end_fill() t.color((255, 155, 192), "white") t.pu() t.seth(90) t.fd(-25) t.seth(0) t.fd(40) t.pd() t.begin_fill() t.circle(15) t.end_fill() t.color("black") t.pu() t.seth(90) t.fd(12) t.seth(0) t.fd(-3) t.pd() t.begin_fill() t.circle(3) t.end_fill() # 腮 t.color((255, 155, 192)) t.pu() t.seth(90) t.fd(-95) t.seth(0) t.fd(65) t.pd() t.begin_fill() t.circle(30) t.end_fill() # 嘴 t.color(239, 69, 19) t.pu() t.seth(90) t.fd(15) t.seth(0) t.fd(-100) t.pd() t.seth(-80) t.circle(30, 40) t.circle(40, 80) # 身体 t.color("red", (255, 99, 71)) t.pu() t.seth(90) t.fd(-20) t.seth(0) t.fd(-78) t.pd() t.begin_fill() t.seth(-130) t.circle(100, 10) t.circle(300, 30) t.seth(0) t.fd(230) t.seth(90) t.circle(300, 30) t.circle(100, 3) t.color((255, 155, 192), (255, 100, 100)) t.seth(-135) t.circle(-80, 63) t.circle(-150, 24) t.end_fill() # 手 t.color((255, 155, 192)) t.pu() t.seth(90) t.fd(-40) t.seth(0) t.fd(-27) t.pd() t.seth(-160) t.circle(300, 15) t.pu() t.seth(90) t.fd(15) t.seth(0) t.fd(0) t.pd() t.seth(-10) t.circle(-20, 90) t.pu() t.seth(90) t.fd(30) t.seth(0) t.fd(237) t.pd() t.seth(-20) t.circle(-300, 15) t.pu() t.seth(90) t.fd(20) t.seth(0) t.fd(0) t.pd() t.seth(-170) t.circle(20, 90) # 脚 t.pensize(10) t.color((240, 128, 128)) t.pu() t.seth(90) t.fd(-75) t.seth(0) t.fd(-180) t.pd() t.seth(-90) t.fd(40) t.seth(-180) t.color("black") t.pensize(15) t.fd(20) t.pensize(10) t.color((240, 128, 128)) t.pu() t.seth(90) t.fd(40) t.seth(0) t.fd(90) t.pd() t.seth(-90) t.fd(40) t.seth(-180) t.color("black") t.pensize(15) t.fd(20) # 尾巴 t.pensize(4) t.color((255, 155, 192)) t.pu() t.seth(90) t.fd(70) t.seth(0) t.fd(95) t.pd() t.seth(0) t.circle(70, 20) t.circle(10, 330) t.circle(70, 30) t.up() t.goto(180, 90) t.pencolor('red') t.write(cls.words, font=('微软雅黑', 15, 'normal')) time.sleep(10) @staticmethod #静态方法 用类名直接调用 不能调用任何类变量/方法、实例变量/方法 def about_me(): print('这个猪类可以画佩奇-Pig.peiQi()') Pig.words = '一个精致的猪猪女孩' #修改类变量 Pig.peiQi() Pig.about_me()