zoukankan      html  css  js  c++  java
  • 面向对象

    类是一个抽象的概念,对象是一个具体的存在

    对象就是数据和函数整合到一起的产物   用嵌套函数进行面向对象设计

     1 def dog(name, gender, type):
     2     def jiao(name):
     3         print('一条叫【%s】的狗,汪汪汪')
     4 
     5     def chi(name):
     6         print('一条叫[%s]的狗,在吃东西')
     7 
     8     def init(name, gender, type):
     9         dic = {
    10             'name': name,
    11             'gender': gender,
    12             'type': type,
    13             'jiao': jiao,
    14             'chi': chi,
    15         }
    16         return dic
    17 
    18     return init(name, gender, type)
    19 
    20 
    21 d1 = dog('于梦', '', '哈士奇')
    22 d1['jiao'](d1)
    23 输出:
    24 一条叫【%s】的狗,汪汪汪

    class Chinese:   这是代表定义了一个经典类

    Class Chinese(object):    这代表定义了一个新式类

    python3  中  都是一样,都是新式类

     1 class Chinese:
     2     dang = '是吗'
     3 
     4     def sui_tan():
     5         print('随地吐痰')
     6 
     7     def chadui(self):
     8         print('插队到前面')
     9 
    10 
    11 print(Chinese.dang)
    12 Chinese.sui_tan()
    13 print(dir(Chinese))
    14 print(Chinese.__dict__)  # 查看类的属性字典
    15 Chinese.__dict__['sui_tan']()
    16 print(Chinese.__name__)   # 类的名字(字符串)
    17 print(Chinese.__doc__)   # 类的文档字符串
    18 print(Chinese.__bases__)  # 祖先
    19 print(Chinese.__module__)  # 显示所在哪个模块
    20 print(Chinese.__class__)  # 实例对应的类
    21 print(Chinese.__dict__)   # 类的属性
    22 输出:
    23 Chinese
    24 None
    25 (<class 'object'>,)
    26 __main__
    27 <class 'type'>
    28 {'__module__': '__main__', 'dang': '共是', 'sui_tan': <function Chinese.sui_tan at 0x009EE268>, 'chadui': <function Chinese.chadui at 0x009EE220>, '__dict__': <attribute '__dict__' of 'Chinese' objects>, '__weakref__': <attribute '__weakref__' of 'Chinese' objects>, '__doc__': None}
  • 相关阅读:
    arcgis 图幅结合图表工具
    Geoprocessor clip
    Spring Boot总结,核心功能,优缺点
    代码下载python 简体和繁体的转换
    电话错误fmxl Missing handler script
    nullnull使用ps绘制像word里面的箭头
    nullnulljava把过滤掉的 % 号拿回来
    问题总结两天来两场实习面试(中科创达、华为)
    模板拷贝【便利贴】Struts性能优化
    匹配优先级Spring攻略学习笔记(3.04)指定Aspect优先级
  • 原文地址:https://www.cnblogs.com/ch2020/p/12423644.html
Copyright © 2011-2022 走看看