绗崄绡囷細缁戝畾鏂规硶涓庨潪缁戝畾鏂规硶
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了绗崄绡囷細缁戝畾鏂规硶涓庨潪缁戝畾鏂规硶相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e5%bf%85%e9%a1%bb' title='蹇呴』'>蹇呴』
settings str 鍒嗕韩 ini 灏辨槸 _id opened port涓€ 绫讳腑瀹氫箟鐨勫嚱鏁板垎鎴愪袱澶х被
涓€锛氱粦瀹氭柟娉曪紙缁戝畾缁欒皝锛岃皝鏉ヨ皟鐢ㄥ氨鑷姩灏嗗畠鏈韩褰撲綔绗竴涓弬鏁颁紶鍏ワ級锛?/strong>
銆€銆€銆€銆€1. 缁戝畾鍒扮被鐨勬柟娉曪細鐢╟lassmethod瑁呴グ鍣ㄨ楗扮殑鏂规硶銆?/strong>
涓虹被閲忚韩瀹氬埗
绫?boud_method(),鑷姩灏嗙被褰撲綔绗竴涓弬鏁颁紶鍏?/strong>
锛堝叾瀹炲璞′篃鍙皟鐢紝浣嗕粛灏嗙被褰撲綔绗竴涓弬鏁颁紶鍏ワ級
銆€銆€銆€銆€2. 缁戝畾鍒板璞$殑鏂规硶锛氭病鏈夎浠讳綍瑁呴グ鍣ㄨ楗扮殑鏂规硶銆?/strong>
涓哄璞¢噺韬畾鍒?/strong>
瀵硅薄.boud_method(),鑷姩灏嗗璞″綋浣滅涓€涓弬鏁颁紶鍏?/strong>
锛堝睘浜庣被鐨勫嚱鏁帮紝绫诲彲浠ヨ皟鐢紝浣嗘槸蹇呴』鎸夌収鍑芥暟鐨勮鍒欐潵锛屾病鏈夎嚜鍔ㄤ紶鍊奸偅涔堜竴璇达級
浜岋細闈炵粦瀹氭柟娉曪細鐢╯taticmethod瑁呴グ鍣ㄨ楗扮殑鏂规硶
銆€銆€ 1. 涓嶄笌绫绘垨瀵硅薄缁戝畾锛岀被鍜屽璞¢兘鍙互璋冪敤锛屼絾鏄病鏈夎嚜鍔ㄤ紶鍊奸偅涔堜竴璇淬€傚氨鏄竴涓櫘閫氬伐鍏疯€屽凡
銆€銆€銆€銆€娉ㄦ剰锛氫笌缁戝畾鍒板璞℃柟娉曞尯鍒嗗紑锛屽湪绫讳腑鐩存帴瀹氫箟鐨勫嚱鏁帮紝娌℃湁琚换浣曡楗板櫒瑁呴グ鐨勶紝閮芥槸缁戝畾鍒板璞$殑鏂规硶锛屽彲涓嶆槸鏅€氬嚱鏁帮紝瀵硅薄璋冪敤璇ユ柟娉曚細鑷姩浼犲€硷紝鑰宻taticmethod瑁呴グ鐨勬柟娉曪紝涓嶇璋佹潵璋冪敤锛岄兘娌℃湁鑷姩浼犲€间竴璇?/strong>
浜?缁戝畾鏂规硶
缁戝畾缁欑被鐨勬柟娉曪紙classmethod锛?/p>
銆€銆€classmehtod鏄粰绫荤敤鐨勶紝鍗崇粦瀹氬埌绫伙紝绫诲湪浣跨敤鏃朵細灏嗙被鏈韩褰撳仛鍙傛暟浼犵粰绫绘柟娉曠殑绗竴涓弬鏁帮紙鍗充究鏄璞℃潵璋冪敤涔熶細灏嗙被褰撲綔绗竴涓弬鏁颁紶鍏ワ級锛宲ython涓烘垜浠唴缃簡鍑芥暟classmethod鏉ユ妸绫讳腑鐨勫嚱鏁板畾涔夋垚绫绘柟娉?/p>
import settings class mysql: def __init__(self,host,port): self.host=host self.port=port @classmethod def from_conf(cls): print(cls) return cls(settings.HOST,settings.PORT) print(MySQL.from_conf) #<bound method MySQL.from_conf of <class 鈥榑_main__.MySQL鈥?gt;> conn=MySQL.from_conf() conn.from_conf() #瀵硅薄涔熷彲浠ヨ皟鐢紝浣嗘槸榛樿浼犵殑绗竴涓弬鏁颁粛鐒舵槸绫?/span>
涓?闈炵粦瀹氭柟娉?/h2>
鍦ㄧ被鍐呴儴鐢╯taticmethod瑁呴グ鐨勫嚱鏁板嵆闈炵粦瀹氭柟娉曪紝灏辨槸鏅€氬嚱鏁?/p>
statimethod涓嶄笌绫绘垨瀵硅薄缁戝畾锛岃皝閮藉彲浠ヨ皟鐢紝娌℃湁鑷姩浼犲€兼晥鏋?/p>
import hashlib import time class MySQL: def __init__(self,host,port): self.id=self.create_id() self.host=host self.port=port @staticmethod def create_id(): #灏辨槸涓€涓櫘閫氬伐鍏?/span> m=hashlib.md5(str(time.time()).encode(鈥?/span>utf-8鈥?/span>)) return m.hexdigest() print(MySQL.create_id) #<function MySQL.create_id at 0x0000000001E6B9D8> #鏌ョ湅缁撴灉涓烘櫘閫氬嚱鏁?/span> conn=MySQL(鈥?/span>127.0.0.1鈥?/span>,3306) print(conn.create_id) #<function MySQL.create_id at 0x00000000026FB9D8> #鏌ョ湅缁撴灉涓烘櫘閫氬嚱鏁?/span>
鍥?classmethod涓巗taticmethod鐨勫尯鍒?/h2>
import settings
class MySQL:
def __init__(self,host,port):
self.host=host
self.port=port
@staticmethod
def from_conf():
return MySQL(settings.HOST,settings.PORT)
# @classmethod #鍝釜绫绘潵璋冪敤,灏卞皢鍝釜绫诲綋鍋氱涓€涓弬鏁颁紶鍏?/span>
# def from_conf(cls):
# return cls(settings.HOST,settings.PORT)
def __str__(self):
return 鈥?/span>灏变笉鍛婅瘔浣?/span>鈥?/span>
class Mariadb(MySQL):
def __str__(self):
return 鈥?/span><%s:%s>鈥?/span> %(self.host,self.port)
m=Mariadb.from_conf()
print(m) #鎴戜滑鐨勬剰鍥炬槸鎯宠Е鍙慚ariadb.__str__,浣嗘槸缁撴灉瑙﹀彂浜哅ySQL.__str__鐨勬墽琛岋紝鎵撳嵃灏变笉鍛婅瘔浣狅細
mariadb鏄痬ysql
import settings class MySQL: def __init__(self,host,port): self.host=host self.port=port @staticmethod def from_conf(): return MySQL(settings.HOST,settings.PORT) # @classmethod #鍝釜绫绘潵璋冪敤,灏卞皢鍝釜绫诲綋鍋氱涓€涓弬鏁颁紶鍏?/span> # def from_conf(cls): # return cls(settings.HOST,settings.PORT) def __str__(self): return 鈥?/span>灏变笉鍛婅瘔浣?/span>鈥?/span> class Mariadb(MySQL): def __str__(self): return 鈥?/span><%s:%s>鈥?/span> %(self.host,self.port) m=Mariadb.from_conf() print(m) #鎴戜滑鐨勬剰鍥炬槸鎯宠Е鍙慚ariadb.__str__,浣嗘槸缁撴灉瑙﹀彂浜哅ySQL.__str__鐨勬墽琛岋紝鎵撳嵃灏变笉鍛婅瘔浣狅細
以上是关于绗崄绡囷細缁戝畾鏂规硶涓庨潪缁戝畾鏂规硶的主要内容,如果未能解决你的问题,请参考以下文章
Windows Presentation Foundation(WPF)涓殑鏁版嵁缁戝畾锛堜娇鐢╔mlDataProvider浣滄帶浠剁粦瀹氾級