Python 类的使用

类的使用

文档化函数:
在函数的开头写下字符串,它会作为函数的一部分运行存储,这称为文档字符串(常用来描述该函数的信息)。
如下:
def test(x,y):
	u"""这是一个Python程序,返回参数x加参数y的值"""
	return  x+y

def test1():
	'this is python program!'
	return 1

print test1.__doc__
print help(test)
运行结果:
this is python program!		#test1.__doc__的打印结果
Help on function test in module __main__:

test(x, y)
    这是一个Python程序,返回参数x加参数y的值

None
[Finished in 0.2s]



类的创建:
# -*-encoding:utf-8 -*-

__metaclass__=type		#新式类的创建方式

class person:
	'create person class and And related properties and methods'

	def setName(self,name):
		'Set the name of the person'
		self.name=name


	def getName(self):
		'get the name of the person'
		return self.name

	def Introduction(self):
		print "my name is %s" % self.name
		print "hello %s ,how are you?" %self.getName()

if __name__=="__main__":
	p=person()
	p.setName('joce')
	p.Introduction()



lambda 关键字

add=lambda x,y : x+y
print add(1,2)
#等价于
def add_test(x,y):
	return x+y
print add_test(10,20)



继承:
class SuperA:       #a的父类
	def add(self,a,b):
		return a+b

	def minues(self,x,y):
		return x-y

class a(SuperA):    #a基础SuperA类

	def show(self,a,b):
		print a,b

newa=a()    #实例化a类
if issubclass(a,SuperA):   #检查a是否为SuperA类     
	result=newa.add(20,30)
	print result

运行结果:
50
[Finished in 0.2s]

 

构造方法:
构造方法是用于主要初始化类的成员属性,构造方法以__init__()方法定义,具体如下实例:
class A:

	def __init__(self,a,b):
		self.a=a
		self.b=b

	def show(self):
		if self.a>self.b:
			print self.a
		else:
			print self.b

a=60
b=20
test=A(a,b)
test.show()
运行结果:
60
[Finished in 0.3s]

子类可以继承父类的构造方法,并获取到父类的成员属性
__metaclass__=type    #新式类定义
class SuperA:

	def __init__(self,a,b):
		self.a=a
		self.b=b

	def size(self):
		if self.a>self.b:
			print self.a
		else:
			print self.b



class A(SuperA):

	def __init__(self):
		super(A,self).__init__(a,b)    #super只能用于新式类
		self.name="python"

	def show(self):
		print self.name


a=90
b=40
testA=A()  #此处类似初始化A类构造方法和父类的构造方法,并将a、b传给父类构造方法
testA.show()
testA.size()
运行结果:
python
90
[Finished in 0.2s]


静态方法:
静态方法的定义没有self参数,且能够被类本身直接调用
__metaclass__=type
class A:

	def show():
		print u"这是静态方法"
	show=staticmethod(show)

	def info(self):
		print u"普通方法"

#第一种调用方式,通过实例化对象调用
test=A()
test.show()
#第二种调用方式,直接通过类名进行调用(普通方法只能通过类实例化对象进行调用)
A.show()
运行结果:
这是静态方法
这是静态方法
[Finished in 0.2s]

通过装饰器定义静态方法:
__metaclass__=type
class A:

	@staticmethod    #静态方法装饰器你
	def show():
		print u"这是静态方法"


	def info(self):
		print u"普通方法"

#第一种调用方式,通过实例化对象调用
test=A()
test.show()
#第二种调用方式,直接通过类名进行调用(普通方法只能通过类实例化对象进行调用)
A.show()
运行结果:
这是静态方法
这是静态方法
[Finished in 0.1s]

类方法:
类方法在定义时需要名为cls的类似于self的参数,类成员方法可以直接用类的具体对象调用,
但cls参数是自动被绑定到的类。
__metaclass__=type
class A:

	@classmethod
	def cmethod(cls):
		print "this is class method",cls
	# cmethod=classmethod(cmethod)


	def info(self):
		print u"普通方法"

#第一种调用方式,通过实例化对象调用
test=A()
test.cmethod()
#第二种调用方式,直接通过类名进行调用(普通方法只能通过类实例化对象进行调用)
A.cmethod()
运行结果:
this is class method <class '__main__.A'>
this is class method <class '__main__.A'>
[Finished in 0.2s]

  

posted @ 2018-12-12 23:23  西夏一品唐  阅读(286)  评论(0编辑  收藏  举报