zoukankan      html  css  js  c++  java
  • 面向对象【林老师版】:如何使用类(八)

    本节内容

    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
  • 相关阅读:
    P2730 魔板 Magic Squares
    P2124 奶牛美容
    4. Median of Two Sorted Arrays(Array; Divide-and-Conquer)
    3.Longest Substring Without Repeating Characters(string; HashTable)
    2.Add Two Numbers (List)
    1.Two Sum (Array; HashTable)
    C++中的浅拷贝、深拷贝、智能指针
    C++ 静态数据成员和静态成员函数
    C & C++ 宏与const
    C++指针与引用
  • 原文地址:https://www.cnblogs.com/luoahong/p/9913693.html
Copyright © 2011-2022 走看看