【1.114】继承 多态 封装
继承 就是为了 多态
不然 就没有必要 写继承
封装 就是一种思想
装 就是编制一个口袋 把要装的东西 如属性 方法 装在一起
封 就是口袋封起来 封闭 隐藏起来
、让我们看到就是一个口袋 需要编制的人 给我们开个口子来看才可以
封装的意思 明确区分内外
一层面意思:
定义类 就是编制一个口袋 放东西进去 就是放到属性字典里
使用者 就要 import 类
或者from 文件 import 类
然后再调用方法
二层面意思:
我们约定,变量前也就是数据属性 一个下划线 就是私有的属性
当我们看到这个下划线就知道这个变量不能被外部访问
但其实这个变量是可以访问的 只是约定给大家不能访问 希望使用者遵守
import sys
dir(sys)
['__breakpointhook__', '__displayhook__', '__doc__', '__excepthook__', '__interactivehook__', '__loader__', '__name__', '__package__', '__spec__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_debugmallocstats', '_enablelegacywindowsfsencoding', '_framework', '_getframe', '_git', '_home', '_xoptions', 'api_version', 'argv', 'base_exec_prefix', 'base_prefix', 'breakpointhook', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'get_asyncgen_hooks', 'get_coroutine_origin_tracking_depth', 'get_coroutine_wrapper', 'getallocatedblocks', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencodeerrors', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'getswitchinterval', 'gettrace', 'getwindowsversion', 'hash_info', 'hexversion', 'implementation', 'int_info', 'intern', 'is_finalizing', 'last_traceback', 'last_type', 'last_value', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'set_asyncgen_hooks', 'set_coroutine_origin_tracking_depth', 'set_coroutine_wrapper', 'setcheckinterval', 'setprofile', 'setrecursionlimit', 'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout', 'thread_info', 'version', 'version_info', 'warnoptions', 'winver']
这里可以调用这个变量,但约定不访问
sys._home
三层意思:
__ :双 下划线 在变量前 如果是双下划线 那就是 隐藏
但是还是可以访问 我们可以通过 __dict__来看属性字典 可以看到
我们用双下划线定义的变量 被 python 修改为 _classname__变量
就是 单下划线 + 类名+双下划线+变量名
浙公网安备 33010602011771号