面向对象编程-廖雪峰
来源:互联网 发布:网络时间同步 编辑:程序博客网 时间:2024/06/11 09:42
限制访问
- 如果要让内部属性不被外部访问,可以把属性的名称前加上两个下划线__
- 如果外部代码要获取name和score怎么办?可以给Student类增加get_name和get_score这样的方法:
> class Student(object):> ...> def get_name(self):> return self.__name> > def get_score(self):> return self.__score> "
- 如果又要允许外部代码修改score怎么办?可以再给Student类增加set_score方法:
class Student(object): ... def set_score(self, score): self.__score = score
获取对象信息
- 基本类型都可以用type()判断:
>>> type(123)<class 'int'>>>> type('str')<class 'str'>>>> type(None)<type(None) 'NoneType'>
- 对于class的继承关系来说,使用type()就很不方便。我们要判断class的类型,可以使用isinstance()函数。
>>> isinstance(h, Husky)True
- 使用dir() 如果要获得一个对象的所有属性和方法,可以使用dir()函数,它返回一个包含字符串的list,比如,获得一个str对象的所有属性和方法:
>>> dir('ABC')['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
0 0
- 面向对象编程-廖雪峰
- 廖雪峰Python教程学习笔记5-面向对象编程
- 【廖雪峰Python习题集】面向对象高级编程
- 廖雪峰Python教程 学习笔记11 面向对象高级编程
- 廖雪峰老师——Python进阶( 面向对象编程基础 )
- 廖雪峰《python3 基础教程》读书笔记——第八章 面向对象编程
- 廖雪峰《python3基础教程》读书笔记——第九章 面向对象高级编程
- 廖雪峰Python教程阅读笔记——6. 面向对象编程
- 廖雪峰Python教程阅读笔记——7. 面向对象高级编程
- 廖雪峰python学习笔记8:面向对象初步
- 廖雪峰python学习笔记9:面向对象高级
- 廖雪峰Python教程1轮还没学明白的(3) -- 面向对象高级编程 -- 定制类 -- __getattr__之链式调用
- 廖雪峰Python教程1轮还没学明白的(1) -- 面向对象高级编程 -- 定制类 -- 斐波那契数列
- 廖雪峰Python教程1轮还没学明白的(2) -- 面向对象高级编程 -- 定制类 -- 实现Fib数列的__getitem__()/切片方法
- 廖雪峰的Python教程-网络编程
- 面向对象/面向过程编程
- 什么是面向对象编程?
- JavaScript 面向对象编程
- iPhone 沙盒路径
- Android UI(Switch)详解
- 第15日看点:中国女排冲击冠军 谌龙PK李宗伟
- 方法调用
- Caffe+Ubuntu 15.04+CUDA 7.0新手安装配置指南
- 面向对象编程-廖雪峰
- Filter过滤器的应用--拦截未登录用户
- Handler消息机制
- UILabel自适应高度
- 页面有中文查询数据库,返回空,properties文件解决中文乱码
- c/c++预处理过程详解(二)之条件编译及预定义的宏
- Cell 的简单自适应高度
- 分布式--简单介绍分布式算法
- 【CSS——必备的基础】