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

本节内容

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
原文地址:https://www.cnblogs.com/luoahong/p/9913693.html