Python 对象知识实践
2017-06-13 12:06
337 查看
#!/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
相关文章推荐
- 《软件工程 ——理论、方法与实践》知识概括第八章 面向对象设计
- 在Python中marshal对象序列化的相关知识
- Python面向对象基础知识
- python内置对象的一些知识
- Python 面向对象基础知识
- Python面向对象的基础知识:类Class,对象(Object),实例(Instance)
- 在Python中marshal对象序列化的相关知识
- python面向对象基本知识
- python面向对象知识杂项
- 机器学习算法与Python实践 - 知识图谱
- 【脚本语言系列】关于Python基础知识对象变动,你知道的事
- Python中关于字符串对象的一些基础知识
- JavaScript基础-对象、继承、传值/址基本知识实践
- Python类基础知识(面向对象基础)
- python知识整理--议可变对象和不可变对象以及str、list、tuple、dict、set
- python零碎知识(5)--对象
- Python3 初学实践案例(10)对象转字典 object to dict
- Java并发编程实践笔记 第一部分 基础知识 第三章 对象的共享:
- 【脚本语言系列】关于Python基础知识对象自省,你需要知道的事
- 《软件工程 ——理论、方法与实践》知识概括第六章 面向对象基础