python-对象、私有属性_方法、继承(多重继承)、重写
来源:互联网 发布:电脑看片有什么软件 编辑:程序博客网 时间:2024/05/22 20:01
对象
#!/usr/bin/python#coding:utf-8class Dog: weight = 5 age = 1.5 def __init__(self): self.weight = 1 self.name = "gou" def run(self): self.weight = 4#构造对象xiaoGou = Dog()#调用属性print xiaoGou.weightprint xiaoGou.ageprint xiaoGou.name#添加属性xiaoGou.color = "red"print xiaoGou.colorxiaoGou.color = "green"print xiaoGou.colorxiaoGou.run()print xiaoGou.weight################print "------------"class Person: # name = "xx" def __init__(self,pName): self.name = pName def __str__(self): return "哈哈。。。"person = Person("person")print person.nameprint person
输出如下:
E:\python\python_jdk\python.exe E:/python/py_pro/safly/Python_Demo.py11.5gouredgreen4------------person哈哈。。。Process finished with exit code 0
私有属性
#!/usr/bin/python#coding:utf-8class Person(object): def __init__(self,name,age): self.name = name self.__age = age def setNewAge(self,newAge): self.__age = newAge def getNewAge(self): return self.__age def __str__(self): return "年龄:"+str(self.__age)person = Person("person",11)print person.nameperson.setNewAge(19)print person.getNewAge()personTemp = personprint id(person) == id(personTemp)print "--NameError: name 'person' is not defined-----"del person# print person
输出如下:
E:\python\python_jdk\python.exe E:/python/py_pro/safly/SaflyDemo.pyperson19True--NameError: name 'person' is not defined-----Process finished with exit code 0
继承、私有方法
#!/usr/bin/python#coding:utf-8class Person(object): def __init__(self,name,age): self.name = name self.__age = age ####私有属性#### def setNewAge(self,newAge): self.__age = newAge ####私有方法#### def __getNewAge(self): return self.__age########调用私有方法####### def getNewAge(self): return self.__getNewAge()class XiaoMing(Person): ########覆盖父类构造方法####### def __init__(self): self.sex = 1person = Person("person",111)#####调用非私有方法#######print person.getNewAge()xiaoming = XiaoMing()#####调用父类方法###xiaoming.setNewAge(11)print xiaoming.getNewAge()print "-----XiaoMing' object has no attribute '__age'-----"# print xiaoming.__age
输出如下:
E:\python\python_jdk\python.exe E:/python/py_pro/safly/SaflyDemo.py11111-----XiaoMing' object has no attribute '__age'-----Process finished with exit code 0
重写方法
#!/usr/bin/python#coding:utf-8class Animal(object): def sing(self): print "啊啊啊啊" def run(self): print "animal run"class Dog(Animal): def sing(self): print "汪汪汪" def run(self): Animal.run(self) super(Dog, self).run() # super().run() 3.xx的特性 print "dog run"dog = Dog()dog.sing()dog.run()
输出如下:
E:\python\python_jdk\python.exe E:/python/py_pro/safly/SaflyDemo.py汪汪汪animal runanimal rundog runProcess finished with exit code 0
多重继承
#!/usr/bin/python#coding:utf-8class A(object): def testA(self): print "---A a---"class B(object): def testB(self): print "---B b----" def testA(self): print "---B a---"class C(A,B): passclass D(B,A): passc = C()c.testA()c.testB()print "---------------"d = D()d.testA()d.testB()
输出如下:
E:\python\python_jdk\python.exe E:/python/py_pro/safly/SaflyDemo.py---A a------B b----------------------B a------B b----Process finished with exit code 0
阅读全文
0 0
- python-对象、私有属性_方法、继承(多重继承)、重写
- 39_面向对象_12_继承_方法的重写
- 继承_方法的重写
- python 继承多重继承
- Python 面向对象 —— 多重继承
- python类:面向对象、继承、slot、多重继承
- 40_面向对象_13_继承_Object类用法_toString和equals方法_重写toString
- 【JavaSE学习笔记】面向对象_继承_方法重写、final关键字
- python 多重继承的方法解析顺序
- python对象的继承和重写
- python对象的继承和重写
- python支持多重继承
- Python多重继承
- python 多重继承
- Python 多重继承mro
- Python 多重继承 mro
- python学习-多重继承
- python的多重继承
- SQlite3软件的安装方法说明(附打包资源)
- 关于Android的.so文件你所需要知道的
- idea中遇到问题解决办法
- 最小二乘法与梯度下降法有哪些区别?
- Sping-Boot 单元测试
- python-对象、私有属性_方法、继承(多重继承)、重写
- WebView的总结
- linux三
- OpenStack大规模部署详解 Ocata
- 题目:解决Python2.7的UnicodeEncodeError: ‘ascii’ codec can’t encode异常错误
- com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 2 of 2-byte
- js 判断数组 对象 为空
- access 学习记录
- 创建第一个Maven项目