類的基本使用


# class MyClass:

#     """A example class"""

#     age = 300

#     def __init__(self,name,age):

#         self.name = name

#         self.age = age

#     def showage(self):

#         print("{}is a student{}".format(self.name,self.age))

# tom = MyClass("lijiabin",18) #實例化 tom是實例

#

# print(tom.age,tom.name) ##tom.age是實例的變量

# print(MyClass.age)

# tom.showage() #實例化後每個實例可以享用對象的方法 類實例化後,得到一個實例對象,實例對象會綁定方法,調用方法時採用jerry.showage()的方式。

#通俗的說就是 實例在調用類的方法時候 自身的屬性特點也會一起帶着!!!!!!!!!!!




#實例變量和類變量

##tom.age是實例的變量

#實例變量是每一個實例自己的變量,是自己獨有的;類變量是類的變量,是類的所有實例共享的屬性和方法




# 特殊屬性 含義

# __name__ 對象名

# __class__ 對象的類型

# __dict__ 對象的屬性的字典

# print(MyClass.__name__)

# print(MyClass.__class__)

# print(type(MyClass))  ##等價於.__class__

# print(MyClass.__dict__.items()) ##__dict__返回一個字典


# print("++++++++++++++++++++++++++")

# tom = MyClass("tp",19)

# print(tom.__class__)

# print(tom.__dict__)  ##實例的字典和屬性  通俗的講就是tom屬於人類 tom的屬性在自己的×××上

# print(type(tom.__class__.__name__))#tom的類的名字 就是一個連環拼接,返回一個字符串

#

#

#

# class Person:

#     age = 3

#     height = 170

#     def __init__(self, name, age=18):

#         self.name = name

#         self.age = age

# tom = Person('Tom') # 實例化、初始化 ¥¥¥¥¥¥¥¥每一個個體調用的時候必須先初始化

# jerry = Person('Jerry', 20)

# Person.age = 30

# print(Person.age, tom.age, jerry.age) # 輸出什麼結果

#

# print(Person.height, tom.height, jerry.height) # 輸出什麼結果

# jerry.height = 175  ###給個體增加屬性 可以在個體的字典裏查到

# print(Person.height, tom.height, jerry.height) # 輸出什麼結果

# # tom.height += 10

# # print(Person.height, tom.height, jerry.height) # 輸出什麼結果

# # Person.height += 15

# # print(Person.height, tom.height, jerry.height) # 輸出什麼結果

# Person.weight = 70

# print(Person.weight, tom.weight, jerry.weight) # 輸出什麼結果

# # print(tom.__dict__['height'])

# print(tom.weight) # 可以嗎

# 指的是實例使用 .點號 來訪問屬性,會先找自己的 __dict__ ,如果沒有,然後通過屬性 __class__ 找到自己的

# 類,再去類的 __dict__ 中找

# 注意,如果實例使用 __dict__[變量名] 訪問變量,將不會按照上面的查找順序找變量了,這是指明使用字典的key

# 查找,不是屬性查找。



#給一個類增加一個屬性可以寫入 也可以通過之前學過的裝飾器來完成 想裝飾器的作用就是給一個函數加一個髮卡 包裝一下這個函數

# 注意書寫等價式


# def decorate(name): #增加姓名

#     def inner(cls): #class

#         cls.Name = name

#         return cls

#     return inner

#

# @decorate("tom")    # Person = decorate("tom")(Person)-> inner(Person)

# class Person:

#     Age = 11

#

# print(Person.__dict__)

#


#類方法

# class Person:

#     @classmethod  #

#     def class_method(cls): # cls是什麼 是類的方法

#         print('class = {0.__name__} ({0})'.format(cls))

#         cls.HEIGHT = 170

#1. 在類定義中,使用@classmethod裝飾器修飾的方法

# 2. 必須至少有一個參數,且第一個參數留給了cls,cls指代調用者即類對象自身

# 3. cls這個標識符可以是任意合法名稱,但是爲了易讀,請不要修改

# 4. 通過cls可以直接操作類的屬性


#

# Person.class_method()

# print(Person.__dict__)




# 類方法的調用

# class Person:

#     def normal_method():

#         print('normal')

#     def method(self):

#         print("{}'s method".format(self))

#     @classmethod  #類方法 給類定義的屬性

#     def class_method(cls): # cls是什麼

#         print('class = {0.__name__} ({0})'.format(cls))

#         cls.HEIGHT = 170

#     @staticmethod   #靜態方法  無需增加參數 靜態方法,只是表明這個方法屬於這個名詞空間。函數歸在一起,方便組織管理

#     def static_methd():

#         print('Person.HEIGHT')



# print('~~~~類訪問')

# print(1,Person.normal_method()) # 可以嗎

# print(2, Person.method()) # 可以嗎 類中的方法是給實例準備的 要穿self才能用 通俗的說就是人類不能去用個體戶的功能,他是每個個體獨有的 要是人類能看 所有個體就都能看 有違邏輯

# print(3, Person.class_method()) # 可以嗎

# print(4, Person.static_methd()) # 可以嗎 keyi

# print(Person.__dict__)

# print('~~~~實例訪問')

# print('tom----')

# tom = Person()

# # print(1, tom.normal_method()) # 可以嗎 不可以 實例必須給定參數 否則不能調用

# print(2, tom.method()) # 可以嗎

# print(3, tom.class_method()) # 可以嗎?

# print(4, tom.static_methd()) # 可以嗎

# print('jerry----')

# jerry = Person()

# # print(1, jerry.normal_method()) # 可以嗎

# print(2, jerry.method()) # 可以嗎

# print(3, jerry.class_method()) # 可以嗎?

# print(4, jerry.static_methd()) # 可以嗎



# 總結:

# 類除了普通方法都可以調用,普通方法需要對象的實例作爲第一參數。

# 實例可以調用所有類中定義的方法(包括類方法、靜態方法),普通方法傳入實例自身,靜態方法和類方法需要找

# 到實例的類。



# 通過方法控制實例的屬性 可以學習一下

# class Person:

#     def __init__(self, name, age=18):

#         self.name = name

#         self.age = age

#     def growup(self, i=1):

#         if i > 0 and i < 150: # 控制邏輯

#             self.age += i

# p1 = Person('tom')

# p1.growup(20) # 正常的範圍

# print(p1.age)

# p1.age = 160 # 超過了範圍,並繞過了控制邏輯

# print(p1.age)




# 補丁

# 可以通過修改或者替換類的成員。使用者調用的方式沒有改變,但是,類提供的功能可能已經改變了。

# 猴子補丁(Monkey Patch):

# 在運行時,對屬性、方法、函數等進行動態替換。

# 其目的往往是爲了通過替換、修改來增強、擴展原有代碼的能力。

# 黑魔法,慎用。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章