1 class People: #python3裏只有新式類 2 eye = 2 #屬性 3 mouth = 1 4 shengao = 180 5 money=1000000 6 def __init__(self,name): 7 self.name = name #構造函數,類在初始化作的一些操做 8 print('造了一我的,這我的是%s'%name) 9 def cry(self): #方法 10 print('哭。。。') 11 def makeMoney(self): #self表明的就是這個類的實例 12 print('self的內存地址',id(self)) 13 print('%s 掙了20w'%self.name ) 14 15 xiaojun = People('小軍') #實例化,xiaojun就是一個實例
1 class Phone: 2 def __del__(self):#析構函數 3 print('哈哈哈哈,我是析構函數!!') 4 def call(self,name): 5 print('爲%s打call,爲%s打電話!'%(name,name)) 6 def __init__(self): 7 self.test = 'testabc' 8 print('我是構造函數') 9 10 iphonx = Phone() 11 print(iphonx.test) 12 iphonx.call('小軍')
#運行結果 我是構造函數 testabc 爲小軍打call,爲小軍打電話! 哈哈哈哈,我是析構函數!!
1 class People: 2 country = 'China' #這是類變量 3 def __init__(self,name,sex): 4 self.name = name #這是實例變量 5 self.sex = sex 6 def say(self): 7 print('name '+self.name) 8 print('sex'+self.sex) 9 print('country'+self.country) 10 11 @property #把一個函數變成一個變量,這個變量的值就是函數的返回值;缺點是不能傳參 12 def get_name(self): 13 return self.name 14 15 print(People.country) 16 xiaojun = People("xiaojun",'男') 17 xiaojun.say() 18 print(xiaojun.get_name) #屬性方法的使用
#運行結果 China name xiaojun sex男 countryChina xiaojun
self表明的就是實例python
1 class People: 2 money=100 3 def __init__(self,name): 4 self.name = name 5 print('造了一我的,這我的是%s'%name) 6 def makeMoney(self): 7 print('self的內存地址',id(self)) 8 print('%s 掙了20w'%self.name ) 9 10 xiaojun = People('小軍') 11 print('小軍的內存地址',id(xiaojun)) #id()查看內存地址 12 xiaojun.makeMoney() 13 People.makeMoney(xiaojun) 14 15 hailong = People('海龍') 16 print('海龍的內存地址',id(hailong)) 17 hailong.makeMoney()
#運行結果,self就是對象自己 造了一我的,這我的是小軍 小軍的內存地址 32212024 self的內存地址 32212024 小軍 掙了20w self的內存地址 32212024 小軍 掙了20w 造了一我的,這我的是海龍 海龍的內存地址 32213592 self的內存地址 32213592 海龍 掙了20w
1 import pymysql 2 3 class MySQL: 4 def __init__(self,host,user,password,db,port=3306,charset='utf8'): #構造函數 5 self.conn = pymysql.connect(host=host,user=user,password=password,db=db,port=port,charset=charset) 6 self.cur = self.conn.cursor(cursor=pymysql.cursors.DictCursor) 7 #初始化的時候就鏈接數據庫 8 def execute_many(self,sql): 9 self.cur.execute(sql) 10 return self.cur.fetchall() # [ {},{} ] 11 def execute_one(self,sql): 12 self.cur.execute(sql) 13 return self.cur.fetchone() 14 def __del__(self): #析構函數 15 self.cur.close() 16 self.conn.close() 17 print('鏈接已經關閉') 18 19 db = MySQL('118.24.3.40','jxz','123456','jxz')
1 class Lw: 2 money = 100000 3 house = '10套' 4 def driver(self): 5 print('開車') 6 def chouyan(self): 7 print('抽菸') 8 def hejiu(self): 9 print('喝酒') 10 def tangtou(self): 11 print('燙頭') 12 13 class Xw(Lw): #Xw繼承了Lw 14 def huaqian(self): 15 print('花錢。。。')
私有變量和私有方法出了類以後就不能訪問了。mysql
1 class DB: 2 port = 3306 #類變量 3 def __init__(self): 4 self.host = '127.0.0.1' 5 self.__user = 'root' #私有變量 6 self.__password = '123456' #私有變量 7 self.db = 'xx' 8 def sql(self): 9 self.__help() #只有類對象本身能訪問,連子類對象也不能訪問到這個方法 10 print('執行sql') 11 def __help(self): #私有方法 12 print(self.host) 13 print(self.__user) #只有類對象本身能訪問,連子類對象也不能訪問到這個數據 14 print(self.__password) 15 print(self.db) 16 print(self.port) 17 18 d = DB() 19 d.sql() 20 d.__help() #AttributeError: 'DB' object has no attribute '__help' 21 d.__user #AttributeError: 'DB' object has no attribute '__user'
1 class DB: 2 port = 3306 #類變量 3 def __init__(self): 4 self.host = '127.0.0.1' #實例變量 5 def get_port(self): #實例方法 6 print(self.port) 7 8 mysql = DB() 9 mysql.get_port() #3306 10 mysql.port = 3307 #不能改類變量,更改的是實例的變量 11 mysql.get_port() #3307 mysql這個實例的port變成了3307 12 print(DB.port) #3306 取類變量 13 oracle = DB() 14 oracle.get_port() #3306 實例裏沒有定義port,依然取類裏面的port值
何時用類方法:sql
1 class DB: 2 port = 3306 3 def __init__(self): 4 self.host = '127.0.0.1' 5 @classmethod #定義類方法 6 def help(cls): 7 print('cls的內存地址', id(cls)) 8 9 print('DB的內存地址', id(DB)) 10 DB.help() #類直接調用,能夠看出cls就是DB
DB的內存地址 41889032 cls的內存地址 41889032
而實例方法:數據庫
何時用靜態方法:json
1 class DB: 2 port = 3306 3 def __init__(self): 4 self.host = '127.0.0.1' 5 @staticmethod #定義靜態方法 6 def about_me(): 7 print('這個類能夠用來xxxxx') 8 DB.about_me()
1 class Lw: 2 def about_me(self,name): 3 print('[%s] 會抽菸喝酒燙頭'%name) 4 5 class Xw(Lw): 6 def about_me(self,name,age): #在原來方法的基礎上增長新功能 7 super().about_me(name) #spuer指的就父類 8 print('age',age) 9 10 def abc(self): 11 pass 12 13 wxm = Xw() 14 wxm.about_me('王小明',18)
[王小明] 會抽菸喝酒燙頭 age 18
1 import requests 2 import nnlog 3 class MyRequest: 4 log_file_name = 'MyRequest.log'#日誌文件名 5 time_out = 10 #請求超時時間 6 def __init__(self,url,data=None,headers=None,file=None): 7 self.url = url 8 self.data = data 9 self.headers = headers 10 self.file = file 11 def post(self): 12 try: 13 req = requests.post(self.url,data=self.data,headers=self.headers, 14 files=self.file,timeout=self.time_out) 15 except Exception as e: 16 res = {"status":0,"err_msg":e.args} #0表明請求失敗 17 else: 18 try: 19 res = {"status":1,"data":req.json()} #1表明返回的json 20 except Exception as e: 21 res = {"staus":2,"data":req.text} #2表明返回不是json 22 log_str = 'url: %s 請求方式:post data:%s ,返回數據:%s'%(self.url,self.data,res) 23 self.write_log(log_str) 24 return res 25 26 def get(self): 27 try: 28 req = requests.get(self.url,params=self.data,headers=self.headers,timeout=self.time_out) 29 except Exception as e: 30 res = {"status":0,"err_msg":e.args} #0表明請求失敗 31 else: 32 try: 33 res = {"status":1,"data":req.json()} #1表明返回的json 34 35 except Exception as e: 36 res = {"staus":2,"data":req.text} #2表明返回不是json 37 log_str = 'url: %s get請求 data:%s ,返回數據:%s'%(self.url,self.data,res) 38 self.write_log(log_str) 39 return res 40 41 @classmethod 42 def write_log(cls,content): 43 log = nnlog.Logger(cls.log_file_name) 44 log.debug(content)