码迷,mamicode.com
首页 > 编程语言 > 详细

Python-面向对象

时间:2018-07-21 20:33:52      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:如何   dict   python   函数   属性   object   __weak   pre   code   

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

标签:如何   dict   python   函数   属性   object   __weak   pre   code   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!