
Class介紹
模組 vs Class
-如果某種東西你只想使用一次,請使用模組。(Singleton)
-避免過度設計資料結構。盡量使用數字、字串、tuple、串列、集合、字典。
(Guido van Rossum)
(一) 基本
1.1 Self
1.2 私用名稱
1.3 Property
(二) 繼承
2.1 覆寫
2.2 Super
(三) 多型
(四) ClassMethod, staticmethod
請注意Python跟C系列的不同。
它的@classmethod、@staticmethod用法,還有MemberData的使用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
class ClassObject(): count = 0 def __init__(self): self.countFail = 1 ClassObject.count += 1
@classmethod def kids(cls): print("A has",cls.count, "little objects.")
@staticmethod def commercial(): print('This coyoteWeapon has been brought to you by Acme')
A = ClassObject() B = ClassObject() C = ClassObject() ClassObject.kids()
ClassObject.commercial()
|
(五) DockType
(六) 特殊用法
eq … etc.
近期评论