首页 > 编程语言 > 详细

Python-面向对象

时间:2018-07-21 20:18:16      阅读:177      评论:0      收藏:0      [点我收藏+]

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-面向对象

原文:https://www.cnblogs.com/friday69/p/9347710.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!