• 面向对象【林老师版】:如何使用类(八)


    本节内容

    1、实现代码

    2、查看类的名称空间

    3、增

    4、删

    5、改

    6、查

    一、实验代码

    class LuffyStudent:   #数据属性
        school = 'luffycity'
    
        def learn(self):  #函数属性
            print('is learning')
        def eat(self):    #函数属性
            print('is sleeping')

    二、查看类的名称空间

    1、代码

    print(LuffyStudent.__dict__)
    print(LuffyStudent.__dict__['school'])
    print(LuffyStudent.__dict__['learn'])

    2、输出

    "C:Program FilesPython35python.exe" "F:/s13/day07/3 如何使用类.py"
    {'__module__': '__main__', '__dict__': <attribute '__dict__' of 'LuffyStudent' objects>, 'learn': <function LuffyStudent.learn at 0x00000000007120D0>, 'eat': <function LuffyStudent.eat at 0x0000000000712268>, 'school': 'luffycity', '__weakref__': <attribute '__weakref__' of 'LuffyStudent' objects>, '__doc__': None}
    luffycity
    <function LuffyStudent.learn at 0x00000000007120D0>
    
    Process finished with exit code 0 

    三、增 

    1、代码

    LuffyStudent.county='China'
    print(LuffyStudent.county)

    2、输出

    "C:Program FilesPython35python.exe" "F:/s13/day07/3 如何使用类.py"
    China
    
    Process finished with exit code 0

    四、删

    1、代码

    del LuffyStudent.county

    五、改

    1、代码

    LuffyStudent.school='Luffycity'
    print(LuffyStudent.school)

    2、输出

    "C:Program FilesPython35python.exe" "F:/s13/day07/3 如何使用类.py"
    Luffycity
    
    Process finished with exit code 0

    六、查

    1、代码

    print(LuffyStudent.school) #LuffyStudent.__dict__['school']
    print(LuffyStudent.learn) #LuffyStudent.__dict__['learn']

    2、输出

    "C:Program FilesPython35python.exe" "F:/s13/day07/3 如何使用类.py"
    luffycity
    <function LuffyStudent.learn at 0x00000000011120D0>
    
    Process finished with exit code 0
  • 相关阅读:
    python并发编程之gevent协程(四)
    python并发编程之asyncio协程(三)
    python并发编程之multiprocessing进程(二)
    python并发编程之threading线程(一)
    python设计模式之内置装饰器使用(四)
    python设计模式之装饰器详解(三)
    python设计模式之迭代器与生成器详解(五)
    EF code First数据迁移学习笔记
    15.02.13-代码小技巧
    Route学习笔记之Area的Route注册
  • 原文地址:https://www.cnblogs.com/luoahong/p/9913693.html
Copyright © 2020-2023  润新知