Python-面向对象
Posted 郭东东郭
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python-面向对象相关的知识,希望对你有一定的参考价值。
1、如何使用类
# 先定义类
class LuffyStudent():
school = "luffycity" # 数据属性
def learn(self): # 函数属性
print("is learning...")
def sleep(self): # 函数属性
print("is sleeping...")
# 查看类的名称空间
print(LuffyStudent.__dict__)
# {'__module__': '__main__', 'school': 'luffycity', 'learn': <function LuffyStudent.learn at 0x00000000025B8B70>, 'sleep': <function LuffyStudent.sleep at 0x00000000025B8BF8>, '__dict__': <attribute '__dict__' of 'LuffyStudent' objects>, '__weakref__': <attribute '__weakref__' of 'LuffyStudent' objects>, '__doc__': None}
print(LuffyStudent.__dict__["school"]) # luffycity
print(LuffyStudent.__dict__["learn"]) # <function LuffyStudent.learn at 0x00000000025B8B70>
# 查
print(LuffyStudent.school) # luffycity
print(LuffyStudent.learn) # <function LuffyStudent.learn at 0x0000000002158AE8>
# 增
LuffyStudent.country = "China"
print(LuffyStudent.country) # China
# 删
del LuffyStudent.country
print(LuffyStudent.__dict__)
# {'__module__': '__main__', 'school': 'luffycity', 'learn': <function LuffyStudent.learn at 0x0000000002158AE8>, 'sleep': <function LuffyStudent.sleep at 0x0000000002158B70>, '__dict__': <attribute '__dict__' of 'LuffyStudent' objects>, '__weakref__': <attribute '__weakref__' of 'LuffyStudent' objects>, '__doc__': None}
# 改
LuffyStudent.school = "foguang University"
print(LuffyStudent.__dict__)
# {'__module__': '__main__', 'school': 'foguang University', 'learn': <function LuffyStudent.learn at 0x0000000002158AE8>, 'sleep': <function LuffyStudent.sleep at 0x0000000002158B70>, '__dict__': <attribute '__dict__' of 'LuffyStudent' objects>, '__weakref__': <attribute '__weakref__' of 'LuffyStudent' objects>, '__doc__': None}
未完待续。。。。。。
以上是关于Python-面向对象的主要内容,如果未能解决你的问题,请参考以下文章