Python基础(六) Python中的类
2015-10-25 20:40
661 查看
Python基础(六) Python中的类
# -*- coding: UTF-8 -*- class Student: #构造函数 def __init__(self,studentId,score): self.studentId = studentId self.score = score self.studentname = "" #成员函数 def displayStudentInfo(self): print "studentid : ",self.studentId," score : ",self.score def setStudentName(self,studentname): self.studentname = studentname def setStudentId(self,studentId): self.studentId = studentId def setStudentScore(self,score): self.score = score guodong = Student("000014",100) guodong.displayStudentInfo() guodong.setStudentName("guodong") print "guodong's studentname : ",guodong.studentname guodong.setStudentId("000018") guodong.setStudentScore(120) guodong.displayStudentInfo() #添加一个属性 guodong.addAttr1 = "addattr1111" #是否存在属性addAttr1 if hasattr(guodong,"addAttr1"): print "guodong.addAttr1 = ",guodong.addAttr1 #添加一个属性 setattr(guodong,'addAttr2', "addattr2222") #是否存在属性addAttr2 if hasattr(guodong,"addAttr2"): print "guodong.addAttr1 = ",guodong.addAttr2 #Python内置类属性 ''' __dict__ : 类的属性(包含一个字典,由类的数据属性组成) __doc__ :类的文档字符串 __name__: 类名 __module__: 类定义所在的模块(类的全名是'__main__.className',如果类位于一个导入模块mymod中,那么className.__module__ 等于 mymod) __bases__ : 类的所有父类构成元素(包含了以个由所有父类组成的元组) ''' print "Student.__doc__:", Student.__doc__ print "Student.__name__:", Student.__name__ print "Student.__module__:", Student.__module__ print "Student.__bases__:", Student.__bases__ print "Student.__dict__:", Student.__dict__
相关文章推荐
- Python基础(五) Python文件操作
- Python基础(四) Python列表
- Python基础(三) Python字符串
- Python基础(二) Python常用变量
- Python基础(一) Hello World
- Python + Requests + Unittest接口自动化测试
- python xlrd对excel的读取功能
- 有意思的Python:开发和部署一览
- Python:使用threading模块实现多线程(转)
- Python多线程学习
- 树莓派上python访问数据库
- python pip的安装和使用
- python对文件进行读写
- python 大文件以行为单位读取方式比对
- Mac osx 使用Python 安装MySQLdb模块
- python 语言常见用法积累
- Python中一个for循环循环多个变量
- Windows下使Python2.x版本的解释器与3.x共存的方法
- Python进阶---python实现substring截取子字符串
- Python进阶---python 中字符串大小写转换