Python 类的应用(1)
来源:互联网 发布:什么叫淘宝pc端 编辑:程序博客网 时间:2024/06/08 07:16
Python 类的应用(1)
首先申明下,本文为笔者学习《Python学习手册》的笔记,并加入笔者自己的理解和归纳总结。
1、类的定义
class语句创建类对象并将其赋值给变量名,类可以看成是模块中的变量。
>>> class Sample: def setValue(self, val): # self指向类实例 self.val = val # 添加一个实例变量val def getValue(self): # 返回实例变量val的值 return self.val
2、创建实例
每个实例都拥有自己的命名空间,实例变量的修改不会影响其他实例。>>> s = Sample() # 在Sample加上括号创建类实例>>> s.setValue("Hello World!") # 调用方法setValue>>> s.getValue()'Hello World!'>>> s1 = Sample() # 创建另一个实例s1>>> s1.setValue("Welcome") # 在s1上调用方法>>> s1.getValue()'Welcome'>>> s.getValue() # s和s1互不干扰'Hello World!'
3、访问变量和方法
(1) 通过逗号(.)访问变量和方法>>> s = Sample()>>> s.val = "Welcome" # 访问实例变量>>> s.getValue()'Welcome'(2) 通过__dict__变量访问变量
>>> s.__dict__ # __dict__是一个字典{'val': 'Welcome'}>>> s.__dict__.get("val")'Welcome'(3) 通过类来访问方法
>>> Sample.setValue(s, "value init") # 类似于s.setValue("value init")>>> s.getValue()'value init'
4、构造函数
新的实例构造时,会调用__init__()方法。>>> class Shape: def __init__(self, x, y, w=10, h=10): self.x = x self.y = y self.width = w self.height = h def draw(self): print "draw"访问构造函数,构造函数同一般的函数一样,可以设默认值,通过关键字访问。
>>> s = Shape(4, 5) # width和height采用默认值>>> s.__dict__{'y': 5, 'x': 4, 'height': 10, 'width': 10}>>> s = Shape(4, 5, 100)>>> s.__dict__{'y': 5, 'x': 4, 'height': 10, 'width': 100}>>> s = Shape(4, 5, w=50, h=45) # 采用混合参数>>> s.__dict__{'y': 5, 'x': 4, 'height': 45, 'width': 50}对于一个已经定义了构造函数的类来说,默认构造方法不一定有效。
>>> s = Shape()Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> s = Shape()TypeError: __init__() takes at least 3 arguments (1 given)
5、类属性
(1) __dict__显示类的属性。>>> s = Shape(4, 5, 50, 45)>>> s.__dict__{'y': 5, 'x': 4, 'height': 45, 'width': 50}>>> Shape.__dict__ # 类显示属性和方法{'__module__': '__main__', 'draw': <function draw at 0x0000000002BF1208>, '__init__': <function __init__ at 0x0000000002BF1198>, '__doc__': None}(2) __bases__显示父类
>>> Shape.__bases__()>>> class Circle(Shape): # Cirle继承Shape类 pass>>> Circle.__bases__(<class __main__.Shape at 0x0000000002BED708>,)>>> class Oval(Shape, Circle): # Oval继承两个类 pass>>> Oval.__bases__(<class __main__.Shape at 0x0000000002BED708>, <class __main__.Circle at 0x0000000002BED768>)
6、类变量
(1) 在Shape中有一个变量color,作为Shape的类变量。>>> Shape.color = "red">>> s1 = Shape(3, 4) # 创建Shape实例s1>>> s2 = Shape(5, 6) # 创建Shape实例s2>>> Shape.color, s1.color, s2.color # 所有实例的color都是一样的('red', 'red', 'red')(2) 修改这个类变量,会影响其他实例。
>>> Shape.color = "blue" # 修改类变量后,所有的实例都被修改>>> Shape.color, s1.color, s2.color('blue', 'blue', 'blue')(3) 如果在某个实例中修改了这个变量,实际会把这个变量本地化,二期不会影响其他实例。
>>> s1.color = "green" # 实例设置color值,color成为实例的本地变量>>> Shape.color, s1.color, s2.color('blue', 'green', 'blue')>>> Shape.color = "red" # 再次修改Shape类的color变量>>> Shape.color, s1.color, s2.color # s1已经设置了本地变量,不会受到影响('red', 'green', 'red')(4) 可以设置方法。
>>> def draw(self): print "position at (%d, %d)" % (self.x, self.y)>>> Shape.draw = draw # 把方法赋值给一个变量>>> Shape.draw(s1)position at (3, 4) # Shape类调用方法>>> s1.draw() # s1实例调用方法position at (3, 4)
7、方法重载
两个相同名称的方法,最后出现的方法会覆盖之前所有的方法,类似于给一个变量赋值,只有最后一次有效>>> class Sample: def add(self, val1, val2): return val1 + val2 def add(self, val1, val2, val3): return val1 + val2 + val3>>> s = Sample()>>> s.add(13, 23) # 调用add方法,add方法被覆盖Traceback (most recent call last): File "<pyshell#71>", line 1, in <module> s.add(13, 23)TypeError: add() takes exactly 4 arguments (3 given)>>> s.add(13, 23, 37)73
8、自定义类的打印信息
(1) class实例会调用默认打印信息。>>> class Sample: pass>>> Sample()<__main__.Sample instance at 0x03302940>(2) 字符串表达式__repr__定义交互式界面的输出。
>>> class Sample: def __repr__(self): return "[Sample, repr]">>> Sample() # 交互式界面调用repr方法[Sample, repr]>>> print Sample() # print方法也会调用repr方法[Sample, repr](3) 字符串表达式__str__定义print的输出。
>>> class Sample: def __str__(self): return "[Sample, str]">>> Sample() # 交互式界面调用默认方法<__main__.Sample instance at 0x0000000002AC8848>>>> print Sample() # print方法调用str方法[Sample, str](4) __repr__和__str__都被定义。
>>> class Sample: def __repr__(self): return "[Sample, repr]" def __str__(self): return "[Sample, str]">>> Sample() # 交互式界面调用repr方法[Sample, repr]>>> print Sample() # print方法调用str方法[Sample, str]
0 0
- Python 类的应用(1)
- python类的组合应用
- Python 类的应用(2)
- Python 语言及其应用 Chapter_6_Note 1 类的super功能
- Python创建自己的邮箱应用(1)
- Python的Django框架应用(1)
- python的实例应用
- Python的应用集合
- python gearman的应用
- Python 的应用
- Python字典的应用
- python的应用范围
- 应用Python的公司
- python的应用
- python的全局变量应用
- python集合的应用
- Python 的应用
- python format的应用
- c语言链表 头指针
- Jvm 调优实战
- 用Java程序往mysql中写数据,出现如下错误
- leetcode No237. Delete Node in a Linked List
- 验证码生成工具:VerifyCodeUtils,controller
- Python 类的应用(1)
- 【Tomcat】Tomcat Session在Redis共享
- 多继承原理抛砖
- PHP合并数组+与array_merge的区别
- Spring MVC带进度条的多文件上传
- Linux系统下搭建DNS服务器——DNS原理总结
- 《flask web开发》第八章 用户认证
- CentOS作为PPTP客户端
- 46. Permutations