zoukankan      html  css  js  c++  java
  • 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}
    复制代码
    学习记录,小白一枚
  • 相关阅读:
    e3.tree参考手册
    fckeditor使用详解
    提示constructor无法location的原因
    无限联动下拉框使用步骤
    动态sql构建的过程
    xsqlbuilder使用说明
    处理date类型对象的方式
    wdatepicker使用指南
    How to reclaim space in InnoDB when innodb_file_per_table is ON
    Bash script: report largest InnoDB files
  • 原文地址:https://www.cnblogs.com/wangsirde0428/p/14322547.html
Copyright © 2011-2022 走看看