上篇:
class tool(object):
# 類屬性
num = 0
# 方法
def __init__(self,name):
# 例項屬性
self.name = name
tool.num += 1
tool1 = tool("a") # 例項物件
tool2 = tool("b") # 例項物件
tool3 = tool("c") # 例項物件
# 類屬性
num = 0
# 例項方法 接受物件
def __init__(self):
# 例項屬性
self.name = "laowang"
# 類方法 對屬性進行修改
@classmethod 接受類的引用
def add_num(cls):
cls.num = 100
# 靜態方法 靜態方法可以不傳引數
@staticmethod
def print_menu():
print("------1-----")
print("------2-----")
print("------3-----")
game = game()
# 通過類的名字呼叫類方法
game.add_num()
# 通過類建立出來的物件呼叫方法
game.add_num()
print(game.num)
100game.print_menu()
game.print_menu()
python學習筆記 25 例項屬性和類屬性
由於python是動態語言,根據類建立的例項可以任意繫結屬性。給例項繫結屬性的方法是通過例項變數,或者通過self變數 class student object def init self,name self.name name s student bob s.score 90 但是,如果stude...
Python學習 例項屬性和類屬性
由於python是動態語言,根據類建立的例項可以任意繫結屬性。給例項繫結屬性的方法是通過例項變數,或者通過self變數 class student object def init self,name self.name name s student bob s.score 90但是,如果studen...
Python類屬性,例項屬性
dreamfor的部落格 1.python類資料屬性 定義在類裡面但在函式外面的變數,它們都是靜態的。一段很簡單的 但反應了很多 class a a 1 乙個類裡面有個屬性a a a b a a.a b.a a.a 這個屬效能被例項和類訪問 a.a 2 b.a a.a 改變例項a的屬性a,例項b和類...