class Employee:         //定义类 以冒号结束
'所有员工的基类' //帮助信息
empCount = 0
def __init__(self, name, salary): //调用时初始化,属性有name和salary
self.name = name
self.salary = salary
Employee.empCount += 1 def displayCount(self):
print "Total Employee %d" % Employee.empCount //%格式化打印格式 def displayEmployee(self):
print "Name : ", self.name, ", Salary: ", self.salary "创建 Employee 类的第一个对象"
emp1 = Employee("Zara", 2000)
"创建 Employee 类的第二个对象"
emp2 = Employee("Manni", 5000)
emp1.displayEmployee()
emp2.displayEmployee()
print "Total Employee %d" % Employee.empCount
emp1.age = 7 # 添加一个 'age' 属性
emp1.age = 8 # 修改 'age' 属性
del emp1.age # 删除 'age' 属性
hasattr(emp1, 'age') # 如果存在 'age' 属性返回 True。
getattr(emp1, 'age') # 返回 'age' 属性的值
setattr(emp1, 'age', 8) # 添加属性 'age' 值为 8
delattr(empl, 'age') # 删除属性 'age' //内置类属性
#!/usr/bin/python
# -*- coding: UTF-8 -*- class Employee:
'所有员工的基类'
empCount = 0
def __init__(self, name, salary):
self.name = name
self.salary = salary
Employee.empCount += 1
def displayCount(self):
print "Total Employee %d" % Employee.empCount
def displayEmployee(self):
print "Name : ", self.name, ", Salary: ", self.salary
print "Employee.__doc__:", Employee.__doc__ //帮助文档
print "Employee.__name__:", Employee.__name__ //类名
print "Employee.__module__:", Employee.__module__ //模块
print "Employee.__bases__:", Employee.__bases__ //类的所有父类构成元素
print "Employee.__dict__:", Employee.__dict__ //类的属性 //
#!/usr/bin/python
# -*- coding: UTF-8 -*- class Parent: # 定义父类
parentAttr = 100
def __init__(self):
print "调用父类构造函数"
def parentMethod(self):
print '调用父类方法'
def setAttr(self, attr):
Parent.parentAttr = attr
def getAttr(self):
print "父类属性 :", Parent.parentAttr class Child(Parent): # 定义子类
def __init__(self):
print "调用子类构造方法" def childMethod(self):
print '调用子类方法 child method' c = Child() # 实例化子类
c.childMethod() # 调用子类的方法
c.parentMethod() # 调用父类方法
c.setAttr(200) # 再次调用父类的方法
c.getAttr() # 再次调用父类的方法
输出结果
调用子类构造方法
调用子类方法 child method
调用父类方法
父类属性 : 200 //继承多个类
class A: # 定义类 A
.....
class B: # 定义类 B
.....
class C(A, B): # 继承类 A 和 B
..... //方法重写
#!/usr/bin/python
class parent():
def mymethod(self):
print 'this is a test'
def bp(self):
print 'how are you'
class child(parent):
def __init__(self):
print 'I am bp'
def mymethod(self):
print 'this is not a test'
c=child()
c.mymethod()
c.bp()
输出结果
I am bp
this is not a test
how are you //私有方法和属性
class JustCounter:
__secretCount = 0 # 私有变量
publicCount = 0 # 公开变量 def count(self):
self.__secretCount += 1
self.publicCount += 1
print self.__secretCount counter = JustCounter()
counter.count()
counter.count()
print counter.publicCount
print counter.__secretCount # 报错,实例不能访问私有变量
输出结果
1
2
2
Traceback (most recent call last):
File "test.py", line 17, in <module>
print counter.__secretCount # 报错,实例不能访问私有变量
AttributeError: JustCounter instance has no attribute '__secretCount'

python基础(三)——类的研究的更多相关文章

  1. Python基础-类的探讨(class)

    Python基础-类的探讨(class) 我们下面的探讨基于Python3,我实际测试使用的是Python3.2,Python3与Python2在类函数的类型上做了改变 1,类定义语法  Python ...

  2. 二十六. Python基础(26)--类的内置特殊属性和方法

    二十六. Python基础(26)--类的内置特殊属性和方法 ● 知识框架 ● 类的内置方法/魔法方法案例1: 单例设计模式 # 类的魔法方法 # 案例1: 单例设计模式 class Teacher: ...

  3. Python 基础 三 反射

    Python 基础 三 反射 今天我们先介绍一下反射这个概念,啥是反射?反射就是自己检测自己.在我们Python的面向对象中的反射是啥意思呢?就是通过字符串的形式操作对象相关的属性.python中的一 ...

  4. python基础——枚举类

    python基础——枚举类 当我们需要定义常量时,一个办法是用大写变量通过整数来定义,例如月份: JAN = 1 FEB = 2 MAR = 3 ... NOV = 11 DEC = 12 好处是简单 ...

  5. python基础——定制类

    python基础——定制类 看到类似__slots__这种形如__xxx__的变量或者函数名就要注意,这些在Python中是有特殊用途的. __slots__我们已经知道怎么用了,__len__()方 ...

  6. python基础(26):类的成员(字段、方法、属性)

    1. 字段 字段:包括普通字段和静态字段,他们在定义和使用中有所区别,而最本质的区别是内存中保存的位置不同. 普通字段属于对象 静态字段属于类 字段的定义和使用: class Province: # ...

  7. 进击的Python【第三章】:Python基础(三)

    Python基础(三) 本章内容 集合的概念与操作 文件的操作 函数的特点与用法 参数与局部变量 return返回值的概念 递归的基本含义 函数式编程介绍 高阶函数的概念 一.集合的概念与操作 集合( ...

  8. Python 基础三 文件 函数

    今天回顾一下之前学的文件操作相关知识点,对于文件的操作,主要有一下几部分构成: 一.文件的基础知识 1.文件操作的基本流程 文件操作其实可以分成三大部分: 1.打开文件,获取文件句柄并赋予一个变量 2 ...

  9. Python基础(十一) 类继承

    类继承: 继承的想法在于,充份利用已有类的功能,在其基础上来扩展来定义新的类. Parent Class(父类) 与 Child Class(子类): 被继承的类称为父类,继承的类称为子类,一个父类, ...

  10. Python基础(9) - 类

    Python 看下面一个简单类: >>> class MyClass(object): ... """ ... this is a class with ...

随机推荐

  1. mysql 数据迁移

    最近线上系统新挂了一次磁盘,需要将系统磁盘下的 mysql 数据目录迁移到 数据盘上. 经过一番考察,mysql在安装时,使用了预编译的二进制tar.gz包.共有两处配置了 datadir属性 /et ...

  2. 每天CSS学习之!important

    通过在CSS属性后面跟上!important参数,就会让该属性的优先级变为最高,不管是在css文件,还是内联style中,就属后面跟了!important这个小弟的属性的优先级最高. 让我们来测试一下 ...

  3. java 2D图形绘制

    package jisuan; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; imp ...

  4. PLY文件格式

    一.PLY简介 PLY文件格式是Stanford大学开发的一套三维mesh模型数据格式,图形学领域内很多著名的模型数据,比如Stanford的三维扫描数据库(其中包括很多文章中会见到的Happy Bu ...

  5. IEDA中彻底删除项目

    删除项目一向比较奇葩,因为当你点击到 该项目名称-->右键 时,并没有 delete 选项,导致我们不知道怎么删除,查找多方文档,得到以下解决: (1)将鼠标移到要删除的 项目名称 上,单击并按 ...

  6. FindResource () RT_HTML 为什么总是出错呢 ?

    #include <windows.h> #include <commdlg.h> #include <ole2.h> BOOL GetHtmlResource(L ...

  7. git 继续前进篇

    * git 输入 git log (--all)命令后出现<END>标记? 按q退出历史记录列表即可 * 继续前一天的  继续推送到github  步骤看图 先 链接到 之前工作区 的 文 ...

  8. oracle语句录

    从表中选出一个某个单位最近的记录 select * from RSDL_SHXX where sbsj in (select max (sbsj) from RSDL_SHXX where DW_ID ...

  9. requests保持登录session ,cookie 和 token

    一.request提供了一个一个叫做session的类,来实现客户端和服务端的会话保持 # coding:utf-8 import requests url = "https://passp ...

  10. django本身提供了runserver,为什么不能用来部署(runserver与uWSGI的区别)

    runserver方法是调试django时经常用到的运行方式,它使用django自带的. WSGI Server 运行,主要在测试和开发使用,并且runserver 开启的方式也是单线程. uWSGI ...