026.Python面向對象類的相關操作以及對象和類的刪除操作


類的相關操作

  1. 定義的類訪問共有成員的成員和方法
  2. 定義的類動態添加公有成員的屬性和方法
  3. 定義的類刪除公有成員的屬性和方法

1 定義一個基本的類

#定義一個類
class Plane():
        #添加一個共有成員屬性
        capitain = "John"
        #添加一個私有成員屬性
        __flight_attendant = 20
        #共有綁定方法
        def fly(self):
                print ("飛機飛行速度更快")
        # 共有普通方法,這個只能是使用類來調用
        def fly2():
                print("飛機是速度最快的交通工具")
#定義的類訪問公有成員的屬性和方法
print(Plane.capitain)
Plane.fly2()

執行

[root@node10 python]# python3 test.py
John
飛機是速度最快的交通工具

類外無法調用一個私有成員

class Plane():
        #添加一個共有成員屬性
        capitain = "John"
        #添加一個私有成員屬性
        __flight_attendant = 20
        #共有綁定方法
        def fly(self):
                print ("飛機飛行速度更快")
        # 共有普通方法,這個只能是使用類來調用
        def fly2():
                print("飛機是速度最快的交通工具")
#定義的類訪問公有成員的屬性和方法
print(Plane.capitain)
Plane.fly2()
print(Plane.__flight_attendant)

執行報錯

普通的方法無法調用,因為形參實參不匹配

class Plane():
        #添加一個共有成員屬性
        capitain = "John"
        #添加一個私有成員屬性
        __flight_attendant = 20
        #共有綁定方法
        def fly(self):
                print ("飛機飛行速度更快")
        # 共有普通方法,這個只能是使用類來調用
        def fly2():
                print("飛機是速度最快的交通工具")
#定義的類訪問公有成員的屬性和方法
print(Plane.capitain)
Plane.fly2()

obj = Plane()
obj.fly2()

調用報錯

 2 定義的類動態添加公有成員屬性和方法

  • 類只有一個,而對象可以實例化多個
  • 多個對象都可以訪問類中的公有成員屬性方法
  • 而類無法訪問對象中的成員
  • 對象和對象之間彼此獨立,資源不共享.
  • 對象可以調用類中公有成員,有使用權,沒有歸屬權(不能修改或者刪除)
class Plane():
        #添加一個共有成員屬性
        capitain = "John"
        #添加一個私有成員屬性
        __flight_attendant = 20
        #共有綁定方法
        def fly(self):
                print ("飛機飛行速度更快")
        # 共有普通方法,這個只能是使用類來調用
        def fly2():
                print("飛機是速度最快的交通工具")
#定義的類訪問公有成員的屬性和方法
print(Plane.capitain)
Plane.fly2()

Plane.logo = "波音747"
res = Plane.__dict__
print (res)

執行

[root@node10 python]# python3 test.py
John
飛機是速度最快的交通工具
{'__module__': '__main__', 'capitain': 'John', '_Plane__flight_attendant': 20, 'fly': <function Plane.fly at 0x7fd70e7900d0>, 'fly2': <function Plane.fly2 at 0x7fd70e790158>, '__dict__': <attribute '__dict__' of 'Plane' objects>, '__weakref__': <attribute '__weakref__' of 'Plane' objects>, '__doc__': None, 'logo': '波音747'}

添加方法

class Plane():
        #添加一個共有成員屬性
        capitain = "John"
        #添加一個私有成員屬性
        __flight_attendant = 20
        #共有綁定方法
        def fly(self):
                print ("飛機飛行速度更快")
        # 共有普通方法,這個只能是使用類來調用
        def fly2():
                print("飛機是速度最快的交通工具")
#定義的類訪問公有成員的屬性和方法
print(Plane.capitain)
Plane.fly2()

Plane.logo = "波音747"
res = Plane.__dict__
print (res)

#添加無參方法
def raining():
        print ("飛機可以人工降雨")
Plane.raining = raining
Plane.raining()

#添加有參方法
def scanning(behavior):
        print ("飛機可以用來"+behavior)

Plane.scanning = scanning
Plane.scanning("偵察敵情")

#通過lambda表達式添加
Plane.save = lambda : print ("飛機可以用來緊急救援")
Plane.save()

print(Plane.__dict__)

#使用對象
obj2 = Plane()
#查看這這對象的屬性是空的
print (obj2.__dict__)
#但是可以調用類的屬性
obj2.fly()

執行

[root@node10 python]# python3 test.py
John
飛機是速度最快的交通工具
{'__module__': '__main__', 'capitain': 'John', '_Plane__flight_attendant': 20, 'fly': <function Plane.fly at 0x7f295f0f80d0>, 'fly2': <function Plane.fly2 at 0x7f295f0f8158>, '__dict__': <attribute '__dict__' of 'Plane' objects>, '__weakref__': <attribute '__weakref__' of 'Plane' objects>, '__doc__': None, 'logo': '波音747'}
飛機可以人工降雨
飛機可以用來偵察敵情
飛機可以用來緊急救援
{'__module__': '__main__', 'capitain': 'John', '_Plane__flight_attendant': 20, 'fly': <function Plane.fly at 0x7f295f0f80d0>, 'fly2': <function Plane.fly2 at 0x7f295f0f8158>, '__dict__': <attribute '__dict__' of 'Plane' objects>, '__weakref__': <attribute '__weakref__' of 'Plane' objects>, '__doc__': None, 'logo': '波音747', 'raining': <function raining at 0x7f295f1d8e18>, 'scanning': <function scanning at 0x7f295f0f81e0>, 'save': <function <lambda> at 0x7f295f0f8268>}
{}
飛機飛行速度更快

3 對象去調用方法

  • 當對象去調用方法時,系統會自動把obj當成參數進行傳遞,fly中的self自動進行接收
  • 在類中要么使用對象.屬性或者方法 要么使用類.屬性或者方法,其他調用情況都是錯誤的
class Plane():
        #添加一個共有成員屬性
        capitain = "John"
        #添加一個私有成員屬性
        __Price = "飛機的價格是5億元"
        #共有綁定方法
        def fly(self):
                print ("飛機飛行速度更快,機長是:",self.capitain)
        # 共有普通方法,這個只能是使用類來調用
        def raining():
                print ("飛機可以人工降雨,機長是",Plane.capitain)

        #私有綁定方法
        def __radar_frequency(self):
                print("雷達的頻率是10萬兆赫,價格是:",self.__Price)

        #私有普通方法
        def plane_price():
                print ("我的飛機飛行很快,價格是:",Plane.__Price)

obj = Plane()
obj.fly()                #obj當成參數進行傳遞,fly中的self自動進行接收
print (obj.capitain)     #self.capitain <==> obj.capitain 

執行

飛機飛行速度更快,機長是: John
John

4 類外調用私有成員

在類外不可以調用私有的成員屬性方法,可以在類內使用公有方法調用私有成員屬性和方法

class Plane():
        #添加一個共有成員屬性
        capitain = "John"
        #添加一個私有成員屬性
        __Price = "飛機的價格是5億元"
        #共有綁定方法
        def fly(self):
                print ("飛機飛行速度更快,機長是:",self.capitain)
        # 共有普通方法,這個只能是使用類來調用
        def raining():
                print ("飛機可以人工降雨,機長是",Plane.capitain)

        #私有綁定方法
        def __radar_frequency(self):
                print("雷達的頻率是10萬兆赫,價格是:",self.__Price)

        #私有普通方法
        def plane_price():
                print ("我的飛機飛行很快,價格是:",Plane.__Price)
obj = Plane()
obj.fly()
print (obj.capitain)
obj. __radar_frequency()

執行

在類內使用公有方法調用私有成員屬性和方法

class Plane():
        #添加一個共有成員屬性
        capitain = "John"
        #添加一個私有成員屬性
        __Price = "飛機的價格是5億元"
        #共有綁定方法
        def fly(self):
                print ("飛機飛行速度更快,機長是:",self.capitain)
        # 共有普通方法,這個只能是使用類來調用
        def raining():
                print ("飛機可以人工降雨,機長是",Plane.capitain)

        #私有綁定方法
        def __radar_frequency(self):
                print("雷達的頻率是10萬兆赫,價格是:",self.__Price)

        #私有普通方法
        def __plane_price():
                print ("我的飛機飛行很快,價格是:",Plane.__Price)
        #公有方法調用私有成員,用對象來調用
        def plane_price_info(self):
                print (self.__Price)
                self.__radar_frequency()
        #也可以使用類來調用
        def plane_price_info2():
                print(Plane.__Price)
                Plane. __plane_price()
obj = Plane()
obj.fly()
print (obj.capitain)
#用對象來調用的方法
obj.plane_price_info()
#用類來調用的方法
Plane.plane_price_info2()

執行

[root@node10 python]# python3 test.py
飛機飛行速度更快,機長是: John
John
飛機的價格是5億元
雷達的頻率是10萬兆赫,價格是: 飛機的價格是5億元
飛機的價格是5億元
我的飛機飛行很快,價格是: 飛機的價格是5億元

5 類外直接調用私有成員

需要用到改名機制:

私有成員的名字 => _類名+私有成員本身
其他語言當中,如果是私有的,無論用什么方式都調用不了.

class Plane():
        #添加一個共有成員屬性
        capitain = "John"
        #添加一個私有成員屬性
        __Price = "飛機的價格是5億元"
        #共有綁定方法
        def fly(self):
                print ("飛機飛行速度更快,機長是:",self.capitain)
        # 共有普通方法,這個只能是使用類來調用
        def raining():
                print ("飛機可以人工降雨,機長是",Plane.capitain)

        #私有綁定方法
        def __radar_frequency(self):
                print("雷達的頻率是10萬兆赫,價格是:",self.__Price)

        #私有普通方法
        def __plane_price():
                print ("我的飛機飛行很快,價格是:",Plane.__Price)
        #公有方法調用私有成員,用對象來調用
        def plane_price_info(self):
                print (self.__Price)
                self.__radar_frequency()
        #也可以使用類來調用
        def plane_price_info2():
                print(Plane.__Price)
                Plane. __plane_price()
obj = Plane()
obj.fly()
print (obj.capitain)
#用對象來調用的方法
obj.plane_price_info()
#用類來調用的方法
Plane.plane_price_info2()
print (Plane.__dict__)
print (Plane._Plane__Price)
obj._Plane__radar_frequency()

執行

[root@node10 python]# python3 test.py
飛機飛行速度更快,機長是: John
John
飛機的價格是5億元
雷達的頻率是10萬兆赫,價格是: 飛機的價格是5億元
飛機的價格是5億元
我的飛機飛行很快,價格是: 飛機的價格是5億元
{'__module__': '__main__', 'capitain': 'John', '_Plane__Price': '飛機的價格是5億元', 'fly': <function Plane.fly at 0x7fa2661090d0>, 'raining': <function Plane.raining at 0x7fa266109158>, '_Plane__radar_frequency': <function Plane.__radar_frequency at 0x7fa2661091e0>, '_Plane__plane_price': <function Plane.__plane_price at 0x7fa266109268>, 'plane_price_info': <function Plane.plane_price_info at 0x7fa2661092f0>, 'plane_price_info2': <function Plane.plane_price_info2 at 0x7fa266109378>, '__dict__': <attribute '__dict__' of 'Plane' objects>, '__weakref__': <attribute '__weakref__' of 'Plane' objects>, '__doc__': None}
飛機的價格是5億元
雷達的頻率是10萬兆赫,價格是: 飛機的價格是5億元

6 刪除對象中或者類的成員

用關鍵字del

  • capitain 默認歸屬於類中的,obj對象可以使用,
  • 但是無權修改或者刪除,除非obj當中也有capitain屬性.

實例化的對象刪除公有成員屬性和方法,定義的類刪除公有成員屬性和方法

class Plane():
        #添加一個共有成員屬性
        capitain = "John"
        #添加一個私有成員屬性
        __Price = "飛機的價格是5億元"
        #共有綁定方法
        def fly(self):
                print ("飛機飛行速度更快,機長是:",self.capitain)
        # 共有普通方法,這個只能是使用類來調用
        def raining():
                print ("飛機可以人工降雨,機長是",Plane.capitain)

        #私有綁定方法
        def __radar_frequency(self):
                print("雷達的頻率是10萬兆赫,價格是:",self.__Price)

        #私有普通方法
        def __plane_price():
                print ("我的飛機飛行很快,價格是:",Plane.__Price)
        #公有方法調用私有成員,用對象來調用
        def plane_price_info(self):
                print (self.__Price)
                self.__radar_frequency()
        #也可以使用類來調用
        def plane_price_info2():
                print(Plane.__Price)
                Plane. __plane_price()
obj = Plane()
obj.fly()
print (obj.capitain)
#用對象來調用的方法
obj.plane_price_info()
#用類來調用的方法
Plane.plane_price_info2()
print (Plane.__dict__)
print (Plane._Plane__Price)
obj._Plane__radar_frequency()
del obj.capitain

執行

 可以定義一個再刪除

class Plane():
        #添加一個共有成員屬性
        capitain = "John"
        #添加一個私有成員屬性
        __Price = "飛機的價格是5億元"
        #共有綁定方法
        def fly(self):
                print ("飛機飛行速度更快,機長是:",self.capitain)
        # 共有普通方法,這個只能是使用類來調用
        def raining():
                print ("飛機可以人工降雨,機長是",Plane.capitain)

        #私有綁定方法
        def __radar_frequency(self):
                print("雷達的頻率是10萬兆赫,價格是:",self.__Price)

        #私有普通方法
        def __plane_price():
                print ("我的飛機飛行很快,價格是:",Plane.__Price)
        #公有方法調用私有成員,用對象來調用
        def plane_price_info(self):
                print (self.__Price)
                self.__radar_frequency()
        #也可以使用類來調用
        def plane_price_info2():
                print(Plane.__Price)
                Plane. __plane_price()
obj = Plane()
obj.fly()
print (obj.capitain)
#用對象來調用的方法
obj.plane_price_info()
#用類來調用的方法
Plane.plane_price_info2()
print (Plane.__dict__)
print (Plane._Plane__Price)
obj._Plane__radar_frequency()
print (obj.__dict__)
obj.capitain = "Json"
print (obj.__dict__)
del obj.capitain
print (obj.__dict__)

執行

飛機飛行速度更快,機長是: John
John
飛機的價格是5億元
雷達的頻率是10萬兆赫,價格是: 飛機的價格是5億元
飛機的價格是5億元
我的飛機飛行很快,價格是: 飛機的價格是5億元
{'__module__': '__main__', 'capitain': 'John', '_Plane__Price': '飛機的價格是5億元', 'fly': <function Plane.fly at 0x7f76ee20e0d0>, 'raining': <function Plane.raining at 0x7f76ee20e158>, '_Plane__radar_frequency': <function Plane.__radar_frequency at 0x7f76ee20e1e0>, '_Plane__plane_price': <function Plane.__plane_price at 0x7f76ee20e268>, 'plane_price_info': <function Plane.plane_price_info at 0x7f76ee20e2f0>, 'plane_price_info2': <function Plane.plane_price_info2 at 0x7f76ee20e378>, '__dict__': <attribute '__dict__' of 'Plane' objects>, '__weakref__': <attribute '__weakref__' of 'Plane' objects>, '__doc__': None}
飛機的價格是5億元
雷達的頻率是10萬兆赫,價格是: 飛機的價格是5億元
{}
{'capitain': 'Json'}
{}

從類刪除,就沒有了

class Plane():
        #添加一個共有成員屬性
        capitain = "John"
        #添加一個私有成員屬性
        __Price = "飛機的價格是5億元"
        #共有綁定方法
        def fly(self):
                print ("飛機飛行速度更快,機長是:",self.capitain)
        # 共有普通方法,這個只能是使用類來調用
        def raining():
                print ("飛機可以人工降雨,機長是",Plane.capitain)

        #私有綁定方法
        def __radar_frequency(self):
                print("雷達的頻率是10萬兆赫,價格是:",self.__Price)

        #私有普通方法
        def __plane_price():
                print ("我的飛機飛行很快,價格是:",Plane.__Price)
        #公有方法調用私有成員,用對象來調用
        def plane_price_info(self):
                print (self.__Price)
                self.__radar_frequency()
        #也可以使用類來調用
        def plane_price_info2():
                print(Plane.__Price)
                Plane. __plane_price()
obj = Plane()
obj.fly()
print (obj.capitain)
print (Plane.capitain)
#刪除capitain
del Plane.capitain
print (Plane.capitain)

執行

刪除方法,直接得了刪除

class Plane():
        #添加一個共有成員屬性
        capitain = "John"
        #添加一個私有成員屬性
        __Price = "飛機的價格是5億元"
        #共有綁定方法
        def fly(self):
                print ("飛機飛行速度更快,機長是:",self.capitain)
        # 共有普通方法,這個只能是使用類來調用
        def raining():
                print ("飛機可以人工降雨,機長是",Plane.capitain)

        #私有綁定方法
        def __radar_frequency(self):
                print("雷達的頻率是10萬兆赫,價格是:",self.__Price)

        #私有普通方法
        def __plane_price():
                print ("我的飛機飛行很快,價格是:",Plane.__Price)
        #公有方法調用私有成員,用對象來調用
        def plane_price_info(self):
                print (self.__Price)
                self.__radar_frequency()
        #也可以使用類來調用
        def plane_price_info2():
                print(Plane.__Price)
                Plane. __plane_price()
obj = Plane()
obj.fly()
print (obj.capitain)
print (Plane.__dict__)
#刪除方法
del Plane.raining
print (Plane.__dict__)

執行

[root@node10 python]# python3 test.py
飛機飛行速度更快,機長是: John
John
{'__module__': '__main__', 'capitain': 'John', '_Plane__Price': '飛機的價格是5億元', 'fly': <function Plane.fly at 0x7fea9ba7d0d0>, 'raining': <function Plane.raining at 0x7fea9ba7d158>, '_Plane__radar_frequency': <function Plane.__radar_frequency at 0x7fea9ba7d1e0>, '_Plane__plane_price': <function Plane.__plane_price at 0x7fea9ba7d268>, 'plane_price_info': <function Plane.plane_price_info at 0x7fea9ba7d2f0>, 'plane_price_info2': <function Plane.plane_price_info2 at 0x7fea9ba7d378>, '__dict__': <attribute '__dict__' of 'Plane' objects>, '__weakref__': <attribute '__weakref__' of 'Plane' objects>, '__doc__': None}
{'__module__': '__main__', 'capitain': 'John', '_Plane__Price': '飛機的價格是5億元', 'fly': <function Plane.fly at 0x7fea9ba7d0d0>, '_Plane__radar_frequency': <function Plane.__radar_frequency at 0x7fea9ba7d1e0>, '_Plane__plane_price': <function Plane.__plane_price at 0x7fea9ba7d268>, 'plane_price_info': <function Plane.plane_price_info at 0x7fea9ba7d2f0>, 'plane_price_info2': <function Plane.plane_price_info2 at 0x7fea9ba7d378>, '__dict__': <attribute '__dict__' of 'Plane' objects>, '__weakref__': <attribute '__weakref__' of 'Plane' objects>, '__doc__': None}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM