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服务器数据导出 csv 并发送到我的本机

    1.在SQL上执行查询并导出操作 '; 2.发送到本机 SQL>system sz idIsNull2.csv;

  2. Cracking The Coding Interview 5.5

    #include <iostream> #include <vector> using namespace std; int getNum1(int N) { int num= ...

  3. Android : VLC for Android 环境搭建及编译

    一.下载VLC源码: git clone https://code.videolan.org/videolan/vlc-android.git 编译apk:   sh compile.sh -a ar ...

  4. 3.9 C++多继承

    参考:http://www.weixueyuan.net/view/6366.html 总结: C++中一个派生类中允许有两个及以上的基类,我们称这种情况为多继承 使用多继承可以描述事物之间的组合关系 ...

  5. poj1062(分区间迪杰斯特拉,内含测试数据,一直wa的同学可以进来看看)

    昂贵的聘礼 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54946   Accepted: 16518 Descripti ...

  6. Delphi直接实现分享图片功能

    procedure TCustomCameraViewDoc.ShareTextClick(Sender: TObject); var FSharingService: IFMXShareSheetA ...

  7. HIVE点滴:group by和distinct语句的执行顺序

    同一条语句之中,如果同时有group by和distinct语句,是先group by后distinct,还是先distinct后group by呢? 先说结论:先group by后distinct. ...

  8. HTTPS双向认证+USB硬件加密锁(加密狗)配置

    环境:  Ubuntu14.04,apache2.4.7, openssl1.0.1f 安装apache2 apt-get install apache2 -y 一般openssl默认已经安装 开启a ...

  9. poj 2251 Dungeon Master (BFS 三维)

    You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...

  10. [转]内存分配malloc, new , heapalloc

    malloc,new,VirtualAlloc,HeapAlloc性能(速度)比较 http://www.cppblog.com/woaidongmao/archive/2011/08/12/1531 ...