Python 对象知识实践
来源:互联网 发布:苹果手机 解压软件 编辑:程序博客网 时间:2024/06/06 02:55
#!/usr/bin/python# -*- coding: UTF-8 -*-class People(object): hahaname = "abc" # 公开静态名称 _hahaage = 1 # 保护静态名称 __hahasex = "难" # 私有静态名称 # 构造函数 def __init__(self, name, age, sex): # 定义实例变量 self.name = name # 公开变量 self._sex = sex # 保护变量 self.__age = age # 私有变量 # 定义实例方法,第一个参数self def sayHello(self): print "sayHello" # 定义类方法,第一个参数cls @classmethod def test1(cls): print "classmethod" # 参数可为空 @staticmethod def test2(): print "staticmethod" # 私有方法双下划线开头 def __test3(self): print "__test3" # 保护方法单下划线开头 def _test4(self): print "_test4" def __cmp__(self, other): return cmp(self.name, other.name) def __str__(self): return "name:{},age:{},sex:{}".format(self.name, self.__age, self._sex)class Student(People): def __init__(self, name, age, sex, student_id): # 调用基类函数 super(Student, self).__init__(name, age, sex) self.__student_id = student_id def __str__(self): return (super(Student, self).__str__() + ",student_id:{}").format(self.__student_id)if __name__ == '__main__': peos = [People(name="whf", age=1, sex="男"), People(name="oo", age=1, sex="男"), Student(name="ada", age=1, sex="男", student_id="20136302")] peos.sort() for peo in peos: print peo
阅读全文
0 0
- Python 对象知识实践
- python知识-面向对象编程
- python零碎知识(5)--对象
- 知识实践
- 知识+实践+思考+心态
- PHP 零碎知识实践
- 知识实践小结
- SOA 实践知识
- python知识
- python知识
- 网络实践类知识笔记
- Unity3D最近所学知识实践
- 面向对象知识 63.89%
- 面向对象相关知识
- 面向对象知识总结
- php面向对象知识
- 面对对象知识小结
- 面向对象入门知识
- android sdk manager 无法更新
- 伪静态与静态
- Apache Kudu:用于实时分析的最佳用例
- 微信小程序开发(四)获取用户openid
- 阿里云上部署java8和hadoop3.0、spark、hive及Mahout
- Python 对象知识实践
- 详解 ESLint 规则,规范你的代码
- 阅读XRecyclerView源码二
- 如何交换两个变量的值
- 多线程学习笔记
- 剑指Offer: (Java实现) 连续子数组的最大和
- HDU5696(分治)
- Performance Analyzer性能分析器
- WePY 在小程序性能调优上做出的探究