zoukankan      html  css  js  c++  java
  • 与属性的深入交流

                                  属性的增删改查

     

    '''创建一个学生类,假设这些学生都是来自同一个学校,同一个班级,学校和班级则可以当作类属性'''
    class Student:
        school="史莱克学院"
        grade="终极一班"
        
        def __init__(self,name,gender,clsID):
            self.name=name
            self.gender=gender
            self.clsID=clsID
            
        def study(self):
            print("good good study,day day up")
        
        def play_games(self):
            print("会玩才会学,不会玩,你叫我怎么学")

    print(Student.__dict__)



    #执行结果为:
    {'__module__': '__main__', 'school': '史莱克学院', 'grade': '终极一班', '__init__': <function Student.__init__ at 0x0000000002AF22F0>, 'study': <function Student.study at 0x0000000002AF2378>,
     'play_games': <function Student.play_games at 0x00000000026C2400>, '__dict__': <attribute '__dict__' of 'Student' objects>, '__weakref__': <attribute '__weakref__' of 'Student' objects>,
    '__doc__': None}




    #为Student类添加一个类属性【添加类属性】
    Student.clscategory="魔法班"
    print(Student.__dict__)
    #执行结果为:
    {'__module__': '__main__', 'school': '史莱克学院', 'grade': '终极一班', '__init__': <function Student.__init__ at 0x00000000027B22F0>, 'study': <function Student.study at 0x00000000027B2378>
    'play_games': <function Student.play_games at 0x00000000026C2400>, '__dict__': <attribute '__dict__' of 'Student' objects>, '__weakref__': <attribute '__weakref__' of 'Student' objects>,
    '__doc__': None,'clscategory': '魔法班'}

    #_________________添加类属性的格式为 类名.newattr=attrvalue



    #删除clscategory属性
    del Student.clscategory
    print(Student.__dict__)
    #执行结果为:
    {'__module__': '__main__', 'school': '史莱克学院', 'grade': '终极一班', '__init__': <function Student.__init__ at 0x0000000002AF22F0>, 'study': <function Student.study at 0x0000000002AF2378>,
     'play_games': <function Student.play_games at 0x00000000026C2400>, '__dict__': <attribute '__dict__' of 'Student' objects>, '__weakref__': <attribute '__weakref__' of 'Student' objects>,
    '__doc__': None}


    #_________________________删除类属性的格式为 del 类名.attr


    #修改grade属性的属性值
    Student.grade="终极三班"
    print(Student.__dict__)
    #执行结果为:
    {'__module__': '__main__', 'school': '史莱克学院', 'grade': '终极三班', '__init__': <function Student.__init__ at 0x0000000002AF22F0>, 'study': <function Student.study at 0x0000000002AF2378>,
     'play_games': <function Student.play_games at 0x00000000026C2400>, '__dict__': <attribute '__dict__' of 'Student' objects>, '__weakref__': <attribute '__weakref__' of 'Student' objects>,
    '__doc__': None}

    s=Student("唐三","男",6100116003) 
    print(s1.__dict__)    

    执行结果为:
    {'name': '唐三', 'gender': '男', 'clsID': 6100116003}

    #添加实例属性age
    s.age=18
    print(s.__dict__)


    #执行结果为
    {'name': '唐三', 'gender': '男', 'clsID': 6100116003, 'age': 18}   

    #添加实例属性的格式 实例名.newattr=attrvalue

    #删除属性age
    del s.age
    print(s.__dict__)    

    执行结果为:
    {'name': '唐三', 'gender': '男', 'clsID': 6100116003} 

    ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
    s.school="超神学院"
    print(s.__dict__)

    执行结果为;
    {'name': '唐三', 'gender': '男', 'clsID': 6100116003, 'age': 18, 'school': '超神学院'}

    实例并不能修改类属性的值,虽然表面上看上去确实修改了类属性的值,其实只是为实例添加了一个属性,属性名与类属性名相同而已。


                                

                             

                             

    在我身后,微笑地活下去吧。
  • 相关阅读:
    SQL学习_WHERE 数据过滤
    SQL学习_SELECT
    读书笔记之_Win10 与Jmeter5.1.1界面兼容:
    appium+python搭建自动化测试框架_TestAPP框架(三)
    appium+python搭建自动化测试框架_Tools安装(一)
    总目录
    解决IntelliJ IDEA卡顿的方法
    SQL Server
    OBDC无法创建sql server连接
    常见问题
  • 原文地址:https://www.cnblogs.com/L-C98/p/9118126.html
Copyright © 2011-2022 走看看