Python中的面向对象编程详解(上)
来源:互联网 发布:单片机怎么打开 编辑:程序博客网 时间:2024/06/10 04:06
本文讲解了创建类、实例化类、类属性、特殊方法内建属性、静态变量属性、实例变量属性、方法属性、静态方法、类方法等内容。
创建类:Python中,使用 class 关键字来创建一个类。简单的类的声明可以是关键字后紧跟类名。
实例化:类名后跟一对圆括号实例化一个类。
类属性
python的属性与Java和C++等面向对象语言不同,python的属性既包括了数据成员还包括函数元素,通过句点符号来访问。
特殊数据内建属性
C.__name__ 类C的名字(字符串)
C.__doc__ 类C的文档字符串
C.__bases__ 类C的所有父类构成的元组
C.__dict__ 类C的属性
C.__module__ 类C定义所在的模块(1.5 版本新增)
方法属性:分为实例方法和类方法.实例方法只属于一个实例;而类方法属于类,也属于实例.
静态方法:静态方法是类级别的方法,不需要实例化类就可以通过类名直接调用.有两种方法定义:
而且如果还需要调用类中其他的静态方法,或者类方法, 要定义成类方法.类方法也有两种方法定义:
this is a test function
name is: aaa
data is: 123
Target.__doc__ is: This is Target definition
target.__doc__ is: This is Target definition
Target.__dict__ is: {'bar1': <classmethod object at 0x029EF390>, 'func2': <staticmethod object at 0x029EF370>, '__module__': '__main__', 'bar2': <classmethod object at 0x029EF3B0>, 'func1': <staticmethod object at 0x0298EB10>, 'staticVar': 'v1.0', 'main': <function main at 0x029E7D70>, '__doc__': 'This is Target definition', '__init__': <function __init__ at 0x029E7D30>}
target.__dict__ is: {'data': 123, 'name': 'aaa'}
staticVar is: v1.0
dir(Target) is: ['__doc__', '__init__', '__module__', 'bar1', 'bar2', 'func1', 'func2', 'main', 'staticVar']
dir(target) is: ['__doc__', '__init__', '__module__', 'bar1', 'bar2', 'data', 'func1', 'func2', 'main', 'name', 'staticVar']
call static method func1
call static method func2
call class method bar1 and access static varible(staticVar): v1.0
call class method bar2 and access static varible(staticVar): v1.0
原文链接:点击打开链接
创建类:Python中,使用 class 关键字来创建一个类。简单的类的声明可以是关键字后紧跟类名。
实例化:类名后跟一对圆括号实例化一个类。
类属性
python的属性与Java和C++等面向对象语言不同,python的属性既包括了数据成员还包括函数元素,通过句点符号来访问。
特殊数据内建属性
C.__name__ 类C的名字(字符串)
C.__doc__ 类C的文档字符串
C.__bases__ 类C的所有父类构成的元组
C.__dict__ 类C的属性
C.__module__ 类C定义所在的模块(1.5 版本新增)
c.__class__ 实例c对应的类
特殊方法内建属性
dir():获得类属性或者实例属性名字列表.
静态变量属性(也称为类变量):直接在class作用域定义(在函数外定义)
动态变量属性(也称为实例变量):python的实例属性与Java和C++等不同.在Java和C++中,实例属性必须首先声明/定义,而python实例属性是动态创建。
设置实例的属性可以在实例创建后任意时间进行,也可以在能够访问实例的代码中进行。构造器init()是设置这些属性的关键点之一。
def __init__(self, name, data): self.name = name self.data = "123'注意:self类似Java的this关键字作用,它是一个指向自身实例的引用。。。
方法属性:分为实例方法和类方法.实例方法只属于一个实例;而类方法属于类,也属于实例.
静态方法:静态方法是类级别的方法,不需要实例化类就可以通过类名直接调用.有两种方法定义:
#1》装饰器(常用)@staticmethod def foo(): print 'call static method'#2》内建函数def foo(): print 'call static method'foo = staticmethod(foo) #静态方法类方法:类方法是类级别的方法, 与静态方法不同的是,它必须显示传入cls类参数;
而且如果还需要调用类中其他的静态方法,或者类方法, 要定义成类方法.类方法也有两种方法定义:
#1》装饰器(常用)@classmethod def bar(cls): print 'call class method and access static varible(staticVar): ', cls.staticVar#2》内建函数def bar(cls): print 'call class method and access static varible(staticVar): ', cls.staticVarbar = classmethod(bar) #类方法实例详解(可以加很多断点,在debug模式下,一步一步查看程序的运行轨迹)
class Target(): #定义类Target 'This is Target definition' #定义__doc__属性 staticVar = 'v1.0' #定义静态变量(类变量) def __init__(self, name = 'default', data = 0): #定义构造函数 self.name = name #实例变量 self.data = data #实例变量 def main(): print "this is a test function" #方法一:用装饰器定义静态方法 @staticmethod def func1(): print 'call static method func1' def func2(): print 'call static method func2' func2 = staticmethod(func2) #方法二:定义静态方法 #方法一:用装饰器定义类方法 @classmethod def bar1(cls): print 'call class method bar1 and access static varible(staticVar):',cls.staticVar def bar2(cls): print 'call class method bar2 and access static varible(staticVar):',cls.staticVar bar2 = classmethod(bar2) #方法二:定义类方法 if __name__ == '__main__': main()#实例化target = Target('aaa', 123)print 'name is: ', target.nameprint 'data is: ', target.data#打印__doc__属性print 'Target.__doc__ is: ', Target.__doc__print 'target.__doc__ is: ', target.__doc__#打印类__dict__属性print 'Target.__dict__ is: ', Target.__dict__#打印实例__dict__属性print 'target.__dict__ is:',target.__dict__#打印静态变量print 'staticVar is: ', Target.staticVar#打印内建函数dir()print 'dir(Target) is: ', dir(Target)print 'dir(target) is: ', dir(target)#调用静态方法Target.func1()Target.func2()#调用类方法Target.bar1()Target.bar2()输出:
this is a test function
name is: aaa
data is: 123
Target.__doc__ is: This is Target definition
target.__doc__ is: This is Target definition
Target.__dict__ is: {'bar1': <classmethod object at 0x029EF390>, 'func2': <staticmethod object at 0x029EF370>, '__module__': '__main__', 'bar2': <classmethod object at 0x029EF3B0>, 'func1': <staticmethod object at 0x0298EB10>, 'staticVar': 'v1.0', 'main': <function main at 0x029E7D70>, '__doc__': 'This is Target definition', '__init__': <function __init__ at 0x029E7D30>}
target.__dict__ is: {'data': 123, 'name': 'aaa'}
staticVar is: v1.0
dir(Target) is: ['__doc__', '__init__', '__module__', 'bar1', 'bar2', 'func1', 'func2', 'main', 'staticVar']
dir(target) is: ['__doc__', '__init__', '__module__', 'bar1', 'bar2', 'data', 'func1', 'func2', 'main', 'name', 'staticVar']
call static method func1
call static method func2
call class method bar1 and access static varible(staticVar): v1.0
call class method bar2 and access static varible(staticVar): v1.0
原文链接:点击打开链接
1 0
- Python中的面向对象编程详解(上)
- Python中的面向对象编程详解(下)
- python面向对象详解(上)
- Python面向对象编程总结(上)
- Python面向对象编程总结(上)
- Python面向对象编程总结(上)
- Lua中的面向对象编程详解
- ## 面向对象编程上 ##
- 面向对象编程(上)
- python面向对象编程
- python 面向对象编程
- python面向对象编程
- python面向对象编程
- Python 面向对象编程
- Python面向对象编程
- Python 面向对象编程
- python--面向对象编程
- python面向对象编程
- 设置label的内边距
- 读取联系人数据库信息
- Ajax实现省市区三级级联(数据来自mysql数据库)
- 深度学习在目标跟踪中的应用
- CUDA编程(二):基本项目建立流程
- Python中的面向对象编程详解(上)
- 设置UITextView的时候文字居中显示了?
- 愚蠢的错误
- json相关知识
- 一名程序员十年技术之路的思考与感悟
- 从无到有系列之HBase-安装部署01
- HDU-4737-A Bit Fun-TwoPointer
- 人心是奇怪的
- 陷入恶性循环需要赶紧跳出来