python3的面向对象
2016-07-19 22:42
489 查看
python的面向对象也很强大,支持多继承,php和java都是单继承,但都可以实现其他接口
self:类似java的this
test.py
执行结果
self:类似java的this
test.py
#!/usr/bin/python3 #基类 class Base: #父类属性 name='' age=60 ##定义私有属性,私有属性在类外部无法直接进行访问 __money=100 #构造函数 def __init__(self,name,age,money): self.data=[]; self.name=name self.age=age self.money=money def add(self,x): self.data.append(x) def addtwice(self,x): self.add(x); self.add(x) #子类 class Child(Base): age=20 def plus(self,a,b): return a+b obj=Child("小名",26,80) obj.add("test") print(obj.data) print(obj.plus(2,3)) print(obj.age) #print(obj.__money) #提示:AttributeError: 'Child' object has no attribute '__money'
执行结果
[root@mail pythonCode]# python3 test.py ['test'] 5 26
相关文章推荐
- Java VS Python
- 以写代学:python 函数
- 二叉树的最大深度(leetcode-104)
- python
- Python logging使用指南
- python核心编程学习笔记-2016-07-19-03-局部变量代替模块变量
- Python——6切片
- Python学习进程(7)字符串
- 以写代学:python shutil模块
- CodeWars---Python第二题
- Python爬虫----网页解析器和BeautifulSoup第三方模块
- python实战之实现excel读取、统计、写入
- python3的错误和异常操作
- Windows下两个python版本怎么分别安装库
- python初级项目课(一)
- python3的模块import操作
- python3的文件操作
- caffe的python接口学习(8):caffemodel中的参数及特征的抽取
- Python操作memecache
- Python学习笔记(一)—NotePad++与插件的安装和环境配置