绗洓绔?闈㈠悜瀵硅薄

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了绗洓绔?闈㈠悜瀵硅薄相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/dict' title='dict'>dict   mem   port   color   big   obj   鐢ㄦ硶   exit   浠g爜鐗囨   

1. 绠€杩伴潰鍚戝璞$殑涓夊ぇ鐗规€с€?/strong>

銆€銆€鍙傝€冮摼鎺?/a>

銆€銆€缁ф壙锛屽皝瑁呭拰澶氭€?/em>

  • 缁ф壙锛?
    • 缁ф壙灏辨槸缁ф壙鐨勭被鐩存帴鎷ユ湁琚户鎵跨被鐨勫睘鎬ц€屼笉闇€瑕佸湪鑷繁鐨勭被浣撲腑閲嶆柊鍐嶅啓涓€閬嶏紝鍏朵腑琚户鎵跨殑绫诲彨鍋氱埗绫汇€佸熀绫伙紝缁ф壙鐨勭被鍙仛娲剧敓绫汇€佸瓙绫汇€?/li>
  • 灏佽锛?
    • 灏佽灏辨槸鎶婄被涓殑灞炴€у拰鏂规硶瀹氫箟涓虹鏈夌殑锛屾柟娉曞氨鏄湪灞炴€у悕鎴栨柟娉曞悕鍓嶅姞鍙屼笅鍒掔嚎锛岃€屼竴鏃﹁繖鏍峰畾涔変簡灞炴€ф垨鏂规硶鍚嶅悗锛宲ython浼氳嚜鍔ㄥ皢鍏惰浆鎹负_绫诲悕__灞炴€у悕锛堟柟娉曞悕锛夌殑鏍煎紡锛屽湪绫荤殑鍐呴儴璋冪敤杩樻槸鐢ㄥ弻涓嬪垝绾垮姞灞炴€у悕鎴栨柟娉曞悕锛屽湪绫荤殑澶栭儴璋冪敤灏辫鐢╛绫诲悕__灞炴€у悕锛堟柟娉曞悕锛夈€傜埗绫荤殑绉佹湁灞炴€у拰鏂规硶锛屽瓙绫绘棤娉曞鍏惰繘琛屼慨鏀广€?/li>
  • 澶氭€侊細
    • 澶氭€佸氨鏄笉鍚岀殑瀵硅薄鍙互璋冪敤鐩稿悓鐨勬柟娉曠劧鍚庡緱鍒颁笉鍚岀殑缁撴灉锛屾湁鐐圭被浼兼帴鍙g被鐨勬劅瑙夛紝鍦╬ython涓澶勪綋鐜扮潃澶氭€侊紝姣斿涓嶇浣犳槸鍒楄〃杩樻槸瀛楃涓茶繕鏄暟瀛楅兘鍙互浣跨敤+鍜?銆?/li>

2. 浠€涔堟槸楦瓙妯″瀷锛?/strong>

銆€銆€楦瓙绫诲瀷锛堣嫳璇細duck typing锛夋槸鍔ㄦ€佺被鍨嬬殑涓€绉嶉鏍笺€傚湪杩欑椋庢牸涓紝涓€涓璞℃湁鏁堢殑璇箟锛屼笉鏄敱缁ф壙鑷壒瀹氱殑绫绘垨瀹炵幇鐗瑰畾鐨勬帴鍙o紝鑰屾槸鐢卞綋鍓嶆柟娉曞拰灞炴€х殑闆嗗悎鍐冲畾銆?/em>濡傛灉鍏锋湁__iter__,__next__灏辨槸杩唬鍣?/p>

3. super 鐨勪綔鐢紵

銆€褰撳瓙绫讳腑鐨勬柟娉曚笌鐖剁被涓殑鏂规硶閲嶅悕鏃讹紝瀛愮被涓殑鏂规硶浼氳鐩栫埗绫讳腑鐨勬柟娉曪紝閭d箞锛屽鏋滄垜浠兂瀹炵幇鍚屾椂璋冪敤鐖剁被鍜屽瓙绫讳腑鐨勫悓鍚嶆柟娉曪紝灏遍渶瑕佷娇鐢ㄥ埌super()杩欎釜鍑芥暟锛岀敤娉曚负super().鍑芥暟鍚?)

4. mro 鏄粈涔堬紵

銆€銆€绫诲悕.mro()鍙互鐩存帴鍒犻櫎褰撳墠绫荤殑缁ф壙鎵ц椤哄簭

  • 瀵逛簬鏀寔缁ф壙鐨勭紪绋嬭瑷€鏉ヨ锛屽叾鏂规硶锛堝睘鎬э級鍙兘瀹氫箟鍦ㄥ綋鍓嶇被锛屼篃鍙兘鏉ヨ嚜浜庡熀绫伙紝鎵€浠ュ湪鏂规硶璋冪敤鏃跺氨闇€瑕佸褰撳墠绫诲拰鍩虹被杩涜鎼滅储浠ョ‘瀹氭柟娉曟墍鍦ㄧ殑浣嶇疆銆傝€屾悳绱㈢殑椤哄簭灏辨槸鎵€璋撶殑銆屾柟娉曡В鏋愰『搴忋€嶏紙Method Resolution Order锛屾垨MRO锛夈€?/li>

5. 浠€涔堟槸 c3 绠楁硶锛?/strong>

銆€銆€c3绠楁硶鏄痯ython鏂板紡绫讳腑鐢ㄦ潵浜х敓mro椤哄簭鐨勪竴濂楃畻娉曘€傚嵆澶氱户鎵跨殑鏌ユ壘瑙勫垯銆?/em>

6. 鍒椾妇闈㈠悜瀵硅薄涓甫鍙屼笅鍒掔嚎鐨勭壒娈婃柟娉曘€?/strong>

銆€銆€__new__锛氬彲浠ヨ皟鐢ㄥ叾瀹冪被鐨勬瀯閫犳柟娉曟垨鑰呯洿鎺ヨ繑鍥炲埆鐨勫璞℃潵浣滀负鏈被鐨勫疄渚嬨€?/em>

銆€銆€ __init__锛?璐熻矗绫荤殑瀹炰緥鍖?/em>

銆€銆€ __call__锛氬璞″悗杈瑰姞鎷彿锛岃Е鍙戞墽琛?

銆€銆€__str__锛歱rint鎵撳嵃涓€涓璞℃椂銆?/em>

銆€銆€__repr__:print鎵撳嵃涓€涓璞℃椂(娌℃湁__str__鏃舵墽琛?銆?/em>

銆€銆€__enter__涓巁_exit__锛氶厤鍚坵ith浣跨敤銆?/em>

銆€銆€__doc__锛氱被鐨勬敞閲婏紝璇ュ睘鎬ф槸鏃犳硶缁ф壙鐨勩€?

銆€銆€__getattr__锛氬湪浣跨敤璋冪敤灞炴€э紙鏂瑰紡銆佸睘鎬э級涓嶅瓨鍦ㄧ殑鏃跺€欒Е鍙?

銆€銆€__setattr__锛氭坊鍔?淇敼灞炴€т細瑙﹀彂瀹冪殑鎵ц

銆€銆€ __dellattr__锛氬垹闄ゅ睘鎬х殑鏃跺€欎細瑙﹀彂

銆€銆€__delete__锛氶噰鐢╠el鍒犻櫎灞炴€ф椂锛岃Е鍙?/em>

7. 鍙屼笅鍒掔嚎鍜屽崟涓嬪垝绾跨殑鍖哄埆锛?/strong>

  • "鍗曚笅鍒掔嚎" 寮€濮嬬殑鎴愬憳鍙橀噺鍙仛淇濇姢鍙橀噺锛屾剰鎬濇槸鍙湁绫诲璞″拰瀛愮被瀵硅薄鑷繁鑳借闂埌杩欎簺鍙橀噺锛?/em>
  • "鍙屼笅鍒掔嚎" 寮€濮嬬殑鏄鏈夋垚鍛橈紝鎰忔€濇槸鍙湁绫诲璞¤嚜宸辫兘璁块棶锛岃繛瀛愮被瀵硅薄涔熶笉鑳借闂埌杩欎釜鏁版嵁銆?/em>

8. 瀹炰緥鍙橀噺鍜岀被鍙橀噺鐨勫尯鍒紵

  • 瀹炰緥鍙橀噺鏄浜庢瘡涓疄渚嬮兘鐙湁鐨勬暟鎹?/em>
  • 绫诲彉閲忔槸璇ョ被鎵€鏈夊疄渚嬪叡浜殑灞炴€у拰鏂规硶

9. 闈欐€佹柟娉曞拰绫绘柟娉曞尯鍒紵

  • 瀹炰緥鏂规硶锛氱涓€涓弬鏁板繀椤绘槸瀹炰緥瀵硅薄锛岄€氬父涓簊elf銆傚疄渚嬫柟娉曞彧鑳界敱瀹炰緥瀵硅薄璋冪敤銆?/em>
  • 绫绘柟娉曪細浣跨敤瑁呴グ鍣ˊclassmethod銆傜涓€涓弬鏁颁负褰撳墠绫荤殑瀵硅薄锛岄€氬父涓篶ls銆傚疄渚嬪璞″拰绫诲璞¢兘鍙互璋冪敤绫绘柟娉曘€?/em>
  • 闈欐€佹柟娉曪細浣跨敤瑁呴グ鍣ˊstaticmethod銆傛病鏈塻elf鍜宑ls鍙傛暟銆傛柟娉曚綋涓笉鑳戒娇鐢ㄧ被鎴栬€呭疄渚嬬殑浠讳綍灞炴€у拰鏂规硶銆傚疄渚嬪璞″拰绫诲璞¢兘鍙互璋冪敤銆?/em>

10. isinstance 鍜?type 鐨勪綔鐢紵

  • 涓よ€呴兘鐢ㄦ潵鍒ゆ柇瀵硅薄鐨勭被鍨?/em>
  • 瀵逛簬涓€涓璞$殑鐩村睘绫诲瀷鍒ゆ柇鍙互浣跨敤type锛屼絾鏄潪鐩村睘绫诲瀷鍒ゆ柇灏遍渶瑕乮sinstance

11. 鏈夌敤杩?with statement锛堣鍙ワ級鍚楋紵瀹冪殑濂藉鏄粈涔堬紵

銆€銆€鍙傝€冮摼鎺?/a>绠€鍗曚娇鐢?/p>

  • 浣跨敤with鍚庝笉绠ith涓殑浠g爜鍑虹幇浠€涔堥敊璇紝閮戒細杩涜瀵瑰綋鍓嶅璞¤繘琛屾竻鐞嗗伐浣溿€備緥濡俧ile鐨刦ile.close()鏂规硶锛屾棤璁簑ith涓嚭鐜颁换浣曢敊璇紝閮戒細鎵цfile.close锛堬級鏂规硶
  • 鍙湁鏀寔涓婁笅鏂囩鐞嗗櫒鐨勫璞℃墠鑳戒娇鐢╳ith锛屽嵆鍦ㄥ璞″唴瀹炵幇浜嗕袱涓柟娉曪細enter()鍜宊_exit__()

12. 涓嬪垪鏁版嵁缁撴瀯涓?鍝竴绉嶆槸涓嶅彲杩唬鐨?/strong>

銆€銆€A. dict銆€銆€B. object銆€銆€C. set 銆€銆€D. str

13. 瀹炵幇涓€涓?Singleton 鍗曚緥绫? 瑕佹眰閬靛惊鍩烘湰璇█缂栫▼瑙勮寖锛堢敤灏介噺澶氱殑鏂瑰紡锛夈€?/strong>

# 1.__new__瀹炵幇鍗曚緥妯″紡
鈥樷€樷€?/span>
class A(object):
    __flag=None
    def __new__(cls, *args, **kwargs):
        if cls.__flag==None:
            cls.__flag=object.__new__(cls)
            # cls.__flag=super(A,self).__new__(cls)
        return cls.__flag

    def __init__(self,name,age):
        self.name=name
        self.age=age

a=A("zhang",12)
print(a.__dict__,id(a))
b=A("yang",13)
print(b.__dict__,id(b))
print(a.__dict__,id(a))
a.age=22
print(b.__dict__,id(b))
print(a.__dict__,id(a))
鈥樷€樷€?/span>


#2銆傝楗板櫒瀹炵幇鍗曚緥妯″紡
鈥樷€樷€?/span>
def wrapper(cls):
    l=[]
    def inner(*args,**kwargs):
        if not l:
            l.append(cls(*args,**kwargs))
        return l[0]

    return inner

@wrapper
class A(object):
    def __init__(self):
        pass
a1=A()
a2=A()
print("绫昏楗板櫒鍗曚緥锛?,id(a1),id(a2))
鈥樷€樷€?/span>

#3.澶栭儴妯″潡绫诲疄渚嬪鍏?/span>
鈥樷€樷€?/span>
class A(object):
    def __init__(self):
        pass
a=A()
from . import a
print(a)
鈥樷€樷€?/span>


# 绾跨▼鍗曚緥
from threading import Thread,Lock
def wrap(func):
    func.__lock__=Lock()
    def inner(*args,**kwargs):
        with func.__lock__:
            return func(*args,**kwargs)
    return inner

class B(object):
    __flag = None
    @wrap
    def __new__(cls, *args, **kwargs):
        if cls.__flag==None:
            cls.__flag=super().__new__(cls)
        return cls.__flag
    def __init__(self):
        pass

b1=B()
b2=B()
print(id(b1),id(b2))



#绾跨▼鍩轰簬__new__鐨勫崟渚?/span>
import threading
class Singleton(object):
    _instance_lock = threading.Lock()

    def __new__(cls, *args, **kwargs):
        if not hasattr(cls, "_instance"):
            with cls._instance_lock:
                if not hasattr(cls, "_instance"):
                    cls._instance = object.__new__(cls)
        return cls._instance

    def __init__(self):
        pass

#浣跨敤鍏冪被鍒涘缓
import threading
class SingletonType(type):
    _instance_lock = threading.Lock()

    def __call__(cls, *args, **kwargs):
        if not hasattr(cls, "_instance"):
            with SingletonType._instance_lock:
                if not hasattr(cls, "_instance"):
                    cls._instance = super().__call__(*args, **kwargs)
        return cls._instance


class Singleton(metaclass=SingletonType):
    def __init__(self):
        pass


obj1 = Singleton()
obj2 = Singleton()
print(obj1 is obj2)

14. 璇锋弿杩?with 鐨勭敤娉? 濡傛灉鑷繁鐨勭被闇€瑕佹敮鎸?with 璇彞, 搴旇濡備綍涔﹀啓?銆€銆€

鍦ㄧ被涓疄鐜颁袱涓柟娉曪細enter()鍜宊_exit__()
class W(object):
    def __init__(self):
        pass
    def __enter__(self):
        print(鈥?/span>杩涘叆with璇彞鈥?/span>)
        return self
        
    def __exit__(self,*args,**kwargs):
        print(鈥?/span>閫€鍑簑ith璇彞鈥?/span>)
        
with W() as w:
    print(鈥?/span>涔嬪墠鈥?/span>)
    print(w)
    print(鈥?/span>涔嬪悗鈥?/span>)

15. python 涓浣曞垽鏂竴涓璞℃槸鍚﹀彲璋冪敤? 閭d簺瀵硅薄鍙互鏄彲璋冪敤瀵硅薄?濡備綍瀹氫箟涓€涓被, 浣垮叾瀵硅薄鏈韩灏辨槸鍙皟鐢ㄥ璞?

  • 浣跨敤callable鍑芥暟鍒ゆ柇銆?/li>
  • 鍙皟鐢ㄥ璞℃湁7绫伙細
    • 鐢ㄦ埛鑷畾涔夊嚱鏁?/li>
    • 鍐呯疆鍑芥暟
    • 鍐呯疆鏂规硶
    • 鏂规硶(瀹氫箟鍦ㄧ被涓殑鍑芥暟)
    • 绫?/li>
    • 绫诲疄渚?濡傛灉绫讳腑瀹氫箟浜哶_call__鏂规硶锛岄偅涔堣繖涓被鐨勫疄渚嬪氨鏄彲璋冪敤瀵硅薄)
    • 鐢熸垚鍣ㄥ嚱鏁?/li>
  • 鍦ㄧ被涓畾涔塤_call__鏂规硶锛屽疄渚嬪璞″姞()鏄嵆璋冪敤__call__鐨勬柟娉?/li>

16. 璇峰疄鐜颁竴涓爤銆?/strong>銆€銆€

"""
鏍堢殑鎶借薄鏁版嵁绫诲瀷瀹氫箟锛氭爤鐨勬娊璞℃暟鎹被鍨嬪簲璇ョ敱浠ヤ笅缁撴瀯鍜屾搷浣滃畾涔夈€傛爤鎿嶄綔濡備笅锛氥€€

    Stack() 鍒涘缓涓€涓┖鐨勬柊鏍堛€?瀹冧笉闇€瑕佸弬鏁帮紝骞惰繑鍥炰竴涓┖鏍堛€?
    push(item)灏嗕竴涓柊椤规坊鍔犲埌鏍堢殑椤堕儴銆傚畠闇€瑕?item 鍋氬弬鏁板苟涓嶈繑鍥炰换浣曞唴瀹广€?
    pop() 浠庢爤涓垹闄ら《閮ㄩ」銆傚畠涓嶉渶瑕佸弬鏁板苟杩斿洖 item 銆傛爤琚慨鏀广€?
    peek() 浠庢爤杩斿洖椤堕儴椤癸紝浣嗕笉浼氬垹闄ゅ畠銆備笉闇€瑕佸弬鏁般€?涓嶄慨鏀规爤銆?
    isEmpty() 娴嬭瘯鏍堟槸鍚︿负绌恒€備笉闇€瑕佸弬鏁帮紝骞惰繑鍥炲竷灏斿€笺€?
    size() 杩斿洖鏍堜腑鐨?item 鏁伴噺銆備笉闇€瑕佸弬鏁帮紝骞惰繑鍥炰竴涓暣鏁般€?
"""
class Stack(object):
    def __init__(self):
        self.stack = []

    def __str__(self):
        return str(self.stack)

    def push(self, item):
        self.stack.append(item)

    def pop(self):
        return self.stack.pop() if self.stack else "Stack is empty!"

    def peek(self):
        return len(self.stack) - 1

    def isEmpty(self):
        return self.stack == []

    def size(self):
        return len(self.stack)


s=Stack()
s.push(1)
s.push(3)
print(s)

17. 鍏充簬 Python 绫荤殑缁ф壙涓嶆纭殑璇存硶鏄?(澶氶€?

銆€銆€A. Python绫绘棤娉曠户鎵?/strong>銆€銆€B. 鍙互缁ф壙, 鏃犳硶鎵ц鐖剁被鐨勬瀯閫犲嚱鏁?/strong>

銆€銆€C. 鍙互鏈夊涓埗绫?銆€銆€   D. 鍙兘鏈変竴涓埗绫?/strong>

18. 瀹炵幇涓€涓?hashtable 绫? 瀵瑰鏆撮湶鐨勬湁 add 鍜?get 鏂规硶, 婊¤冻浠ヤ笅娴嬭瘯浠g爜銆€銆€

 1 def test():
 2     import uuid
 3     name = {"name", "web", "python"}
 4     ht = HashTable()
 5      for key in names:
 6         value = uuid.uuid4()
 7         ht.add(key,value)
 8         print("add鍏冪礌",key,value)    
 9 
10      for key in names:
11          v = ht.get(key)
12          print("get 鍏冪礌",key, v)
class HashTable(object):
    def __init__(self):
        self.dic = {}
    def add(self,key,value):
        self.dic[key] = value
    def get(self,key):
        return self.dic.get(key)

19. 璇风敤涓や釜闃熷垪鏉ュ疄鐜颁竴涓爤(缁欏嚭浼唬鐮佸嵆鍙?

class Stack(object):
    def __init__(self):
        self.head = []
        self.tail = []

    def push(self, x):
        self.head.append(x)

    def pop(self):
        if self.head == []:
            return None
        while len(self.head) > 1:
            self.tail.append(self.head.pop(0))
        self.head, self.tail = self.tail, self.head
        return self.tail.pop()

    def __str__(self):
        return str(self.head)


s = Stack()
s.push(2)
s.push(333)
s.push(3)
s.pop()
s.pop()
s.push(1)
print(s)

20. 宸茬煡濡備笅閾捐〃绫? 璇峰疄鐜板崟閾捐〃閫嗙疆

1 class Node:
2     def __init__(self, value, next):
3     self.value = value
4     self.next = next
class LinkReverse(object):
    def reverse(self, head):
        if not head or not head.next:return head#濡傛灉閾捐〃涓虹┖鎴栬€呭彧鏈変竴涓厓绱犳槸杩斿洖鏈韩
        #鍙嶈浆鍏宠仈琛ㄧ殑閮芥寚閽堝畾涔?/span>
        last = None
        #灏嗗師閾捐〃鐨勮妭鐐规寚鍚戣繘琛岀炕杞?/span>
        while head:
            temp = head.next
            head.next = last
            last = head
            head = temp
        return last

21. 绫荤殑鍔犺浇椤哄簭(绫讳腑鏈夌户鎵挎湁鏋勯€犳湁闈欐€?锛?/strong>

 

  1. 鍦ㄥ爢鍐呭瓨涓敓鎴恈lass瀵硅薄, 鎶婇潤鎬佸彉閲忓拰闈欐€佹柟娉曞姞杞藉埌鏂规硶鍖? 杩欎釜鍫嗗唴瀛樹腑鐨刢lass瀵硅薄鏄柟娉曞尯鏁版嵁鐨勫叆鍙?/em>
  2. 闈欐€佸彉閲忛粯璁ゅ垵濮嬪寲
  3. 闈欐€佸彉閲忔樉寮忓垵濮嬪寲
  4. 鎵ц闈欐€佷唬鐮佸潡
  5. 鎴愬憳鍙橀噺榛樿鍒濆鍖? 鏄剧ず鍒濆鍖?/em>
  6. 鎵ц鏋勯€犲嚱鏁?/em>

22. 鍙傝€冧笅闈唬鐮佺墖娈点€€銆€

class Context:
    pass

with Content() as ctx:
    ctx.do_something()

銆€銆€璇峰湪 Context绫讳笅娣诲姞浠g爜瀹屾垚璇ョ被鐨勫疄鐜般€€銆€

class Context:
    def do_something(self):
        # print(1122)
        pass
    def __enter__(self):
        return self
    def __exit__(self, exc_type, exc_val, exc_tb):
        pass

with Context() as ctx:
    ctx.do_something()

23. 浠ヤ笅浠g爜杈撳嚭鏄粈涔? 璇风粰鍑虹瓟妗堝苟瑙i噴銆?/strong>銆€銆€

 1 class Parent(object):
 2     x = 1
 3 
 4 class Child1(Parent):
 5     pass
 6 
 7 class Child2(Parent):
 8     pass
 9 
10 print Parent.x, Child1.x, Child2.x
11 
12 Child1.x = 2
13 print Parent.x, Child1.x, Child2.x
14 
15 Parent.x = 3
16 print Parent.x, Child1.x, Child2.x

1 1 1
1 2 1
3 2 3

24. 鍑芥暟 del_node(self,data)鐨勫姛鑳? 鍦ㄦ牴鑺傜偣鎸囬拡涓?root 鐨勪簩鍙夋爲(鍙堢О浜屽弶鎺掑簭鏍?涓婃帓闄ゆ暟鍊间负 K 鐨勮妭鐐?鑻ュ垹闄ゆ垚鍔?杩斿洖 0,鍚﹀垯杩斿洖-1, 姒傝堪鑺傜偣鐨?瀹氫箟绫诲瀷涓恒€€銆€

1 class Node(object):
2     def __init__(self,data):
3         self.data = data # 鑺傜偣鐨勬暟鍊?/span>
4         self.left_child = Node # 鎸囧悜宸﹀彸瀛愭爲鐨勬寚閽?/span>
5         self.right_child = Node
6 
7     def set_data(self,data):
8         self.data = data

25. 璇风粰鍑轰笅闈唬鐮佺墖娈电殑杈撳嚭锛岃绠€杩颁笂闈唬鐮侀渶瑕佹敼杩涚殑鍦版柟锛熴€€銆€

 1 class Singleton:
 2     _instance = None
 3 
 4     def __new__(cls, *args, **kwargs):
 5         print("New")
 6         if cls._instance is None:
 7             print("Create")
 8             cls._instance = super().__new__(cls, *args, **kwargs)
 9         return cls._instance
10 
11     def __init__(self):
12         print("Initalize")
13         self.prop = None
14 
15 
16 s1 = Singleton()
17 s2 = Singleton()

New
Create
Initalize
New
Initalize

26.璇风畝鍗曡В閲?/span>Python涓殑staticmethod(闈欐€佹柟娉?/span>)鍜?/span>classmethod(绫绘柟娉?/span>),骞跺皢浠ヤ笅浠g爜濉啓瀹屾暣銆?/strong>銆€銆€

 1 class A(object):
 2     def foo(self,x)
 3         print 鈥?/span>executing foo(%s, %s)鈥?/span>%(self,x)
 4 
 5     @classmethod
 6     def class_foo(cls,x):
 7         print 鈥?/span>executing class_foo(%s, %s)鈥?/span>%(cls,x)
 8 
 9     @staticmethod
10     def static_foo(x):
11         print 鈥?/span>executing static_foo(%s)鈥?/span>%(x)
12 
13 a= A()
14 # 璋冪敤 foo鍑芥暟,鍙傛暟浼犲叆 1
15 a.foo(1)
16 # 璋冪敤 class_foo鍑芥暟,鍙傛暟浼犲叆 1
17 A.class_foo(1)
18 # 璋冪敤 static_foo鍑芥暟,鍙傛暟浼犲叆 1
19 A.static_foo(1)銆€/  a.ststic_foo(1)

27. 宸茬煡涓€涓鍗曞璞★紙tradeOrder锛夋湁濡備笅瀛楁锛?/strong>銆€銆€

瀛楁鑻辨枃鍚?/p>

涓枃鍚?/p>

瀛楁绫诲瀷

鍙栧€?/p>

Id

涓婚敭

Long

123456789

Name

濮撳悕

String

寮犱笁

Items

鍟嗗搧鍒楄〃闆嗗悎

List<鍟嗗搧>(鍏宠仈鍟嗗搧)

鏌ユ壘鍟嗗搧瀵硅薄锛屼竴涓鍗曟湁涓や釜鍟嗗搧銆傚晢鍝佸瓧娈典换鎰忓彇鍊?/p>

IsMember

鏄惁鏄細鍛?/p>

Boolean

True

CouponAmount

浼樻儬鍒搁噾棰?/p>

Bigdecimal

Null

銆€銆€

銆€銆€銆€銆€鍟嗗搧瀵硅薄

瀛楁鑻辨枃鍚嶇О     

涓枃鍚?/td> 瀛楁绫诲瀷 鍙栧€?/td>
Id 涓婚敭 Long 987654321
Name 鍟嗗搧鍚嶇О String 鎵嬫満

銆€銆€銆€銆€闂锛氳嫢灏嗚鍗曞璞¤浆鎴?JSON 鏍煎紡锛岃涔﹀啓鍑鸿浆鎹㈠悗鐨?JSON 瀛楃涓层€?/p>

 

28. 鍐欎唬鐮?鏍堜笌闃熷垪)

銆€銆€缂栫▼瀹炵幇涓€涓厛杩涘厛鍑虹殑闃熷垪绫? 鑳芥寚瀹氬垵濮嬪寲鏃剁殑闃熷垪澶у皬, 浠ュ強enqueue,dequeue,isempty, isfull 鍥涚鏂规硶

銆€銆€浣跨敤鏂规硶濡備笅:銆€銆€銆€銆€

1 s = Queue(2) # 鍒濆鍖栦竴涓ぇ灏忎负 2鐨勯槦鍒?/span>
2 s.is_empty() # 鍒濆鍖栧悗, 闃熷垪涓虹┖, 杩斿洖 True
3 s.enqueue(1) # 灏?1鍔犲叆闃熷垪
4 s.enqueue(2) # 灏?2鍔犲叆闃熷垪
5 s.isfull() # 鍔犲叆浜嗕袱涓厓绱? 闃熷垪宸叉弧, 杩斿洖 True
6 s.dequeue() # 绉婚櫎涓€涓厓绱? 杩斿洖 1
7 s.dequeue() # 绉婚櫎涓€涓厓绱? 杩斿洖 2
8 s.is_empty() # 闃熷垪宸茬粡涓虹┖, 杩斿洖 True

 

class Queue(object):
    def __init__(self, size):
        self.__size = size
        self.__queue = []

    def enqueue(self, x):
        if len(self.__queue) < self.__size:
            self.__queue.insert(0,x)
        else:
            return 鈥?/span>The queue is full!!!鈥?/span>

    def dequeue(self):
        return None if not len(self.__queue) else self.__queue.pop()

    def is_empty(self):
        return not len(self.__queue)

    def isfull(self):
        return len(self.__queue) == self.__size

s = Queue(2)
print(s.is_empty())
s.enqueue(1)
s.enqueue(2)
print(s.isfull())
print(s.dequeue())
print(s.dequeue())
print(s.is_empty())

 

以上是关于绗洓绔?闈㈠悜瀵硅薄的主要内容,如果未能解决你的问题,请参考以下文章

闈㈠悜瀵硅薄缂栫▼

闈㈠悜瀵硅薄

2.闈㈠悜瀵硅薄涓夊ぇ鐗瑰緛

闈㈠悜瀵硅薄涓夊ぇ鐗规€т箣灏佽

20192328鐗涙钀?2019-2020-1 銆婃暟鎹粨鏋勪笌闈㈠悜瀵硅薄绋嬪簭璁捐銆嬪疄楠屽叚鎶ュ憡

Java瀛︿範绗旇1