python 定义类的方法理解一

在面向对象编程中,变量和函数聚合到一个通用的对象中,可以在任何程序中使用通用的对象。以前使用的过程式编程,在代码中创建变量和函数来指定特定的步骤,例如把值存储到变量中,然后使用结构化语句检查他们。
接下来主要介绍如何定义一个类
一:如何创建对象类
二:定义类的属性和方法
三:如何在python脚本中使用类
四:如何在python脚本中使用类模块

一:创建对象类
1.1首先在python中定义一个类与定义一个函数没有什么不同。定义一个类,使用关键字class,接着是类的名称,然后是一个冒号,其次是任何应该包含在类中的语句。如下示例:

class Product:
	pass

pass占位符 ,创建了一个空的类定义,他不会做什么事情,方便将来添加内容。
1.2定义了这个类,但是没有把类投入使用。要使用一个类必须初始化它。每一个实例都代表一个对象出现,要实例化一个对象,只需要按照名称来调用它。如下:

prod1 = Product()        ''' 变量prod1现在是Product类的一个实例。现在可以动态的定义属性,如下:'''
prod1.description = 'carrot'
print(prod1.description)
carrot

每一个实例都是一个单独的对象。当创建第二个对象,在那个实例中定义的属性与第一个是不同的。

prod2 = Product()
prod2.description = 'dog'
print(prod2.description)
dog
print(prod1.description)
carrot

二 定义类方法
2.1设值方法
2.2访问方法
2.3辅助方法

2.1设值方法
用来改变值的函数,通常设值方法叫做setter。使用一个设值方法来给一个类中的属性赋值。

def  set-description(self,desc):
	self.__description = desc

set_方法使用两个参数。第一个参数是一个特殊值叫做self,他将类指向这个对象的当前的实例。这个参数对于所有类方法是必须的,作为其第一个参数。第二个参数是定义一个值用来设置实例的属性。注意方法语句中使用的赋值语句:

  self.__description = desc

属性名是__description。在属性名前使用两个下划线来表示不能再类定义之外使用它。
也可以在设值方法中包含对属性的某种运算:

 def buy_Product(self,amount):
	self.__inventory = self.__inventory - amount

2.2访问方法
用来访问在类中定义的属性的方法。创建特殊的方法来检索当前的属性,方法叫做getter.
def get_description(self):
return self.__description
尽管没有数据传递给访问方法,但是他仍然需要包含一个self关键字作为参数。下面展示为Product类创建getter和setter方法,然后再一个程序里使用它们。
/home/bin/python13

class Product:
	def  set_description(self,desc):
		self.__description = desc
	def get_description(self):
		return self.__description
	def  set_price(self,price):
		self.__price= price
	def get_price(self):
		return self.__price
	def  set-inventory(self,inventory):
		self.__inventory = inventory
	def  get_inventory(self):
		return self.__inventory
prod1 = Product()
prod1.set_description('carrot')
prod1.set_price(1.00)
prod1.set_inventory(10)
print('{0} - price: ${1: .2f}, inventory: {2: d}'.format(prod1.get_description() ,prod1.get_price() , prod1.get_inventory()))

运行程序后,可以看到来自实例的值

输出:

carrot  -  price:  $1.00 ,  inventory:  10

2.3辅助方法
2.3.1构造函数
使用设值方法set_来设置每一个属性的值,已经变得相当古老了,尤其是当类有众多的属性的时候。使用类的构造函数是用默认值来实例化类的实例的一种简单流行的方法。
Python提供了一种特殊的方法叫做__init__(),当实例化一个新的类实例时,会调用它。可以在类代码中定义__init__()方法,来执行创建实例所要做的工作,包括为属性赋默认值。
方法__init__()需要为每一个想要定义值的属性定义一个参数。如下:

class Product:
	def __init__(self,description,price,inventory):
		self.__description = description
		self.__price = price
		self.__inventory = inventory
	def get_description(self):
		return self.__description
	def get_price(self):
		return self.__price
	def get_inventory(self):
		return self.__inventory

现在,创建Product类的实例时,必须在类的构造函数中定义初始值:

prod3 = Product(‘tomato' , 2.00, 20)
print(' {0} - price : ${1:.2f}, inventory: {2:d}'.format(prod3.get_description() , prod3.get_price() ,prod3.get_inventory()))

输出:

tomato - price :  $2.00,  inventory :  20

这使得创建一个新的实例变得简单,唯一的缺点是必须要指定默认值,如果不这样做的话,就会得到一条错误消息,如下:

prod1 = Product ()
Traceback (most recent call last):
	File"<stdin>", line1 , in <moudle>
	prod1 = Product()
TypeError:  __init__()  takes  exactly  4  arguments  (1 given) 

为了解决这个问题,构造方法就像Python函数一样,允许通过参数定义默认值,如下:

class Product:
	def __init__(self,description = ’new',price = 0,inventory = 0):
		self.__description = description
		self.__price = price
		self.__inventory = inventory
	def get_description(self):
		return self.__description
	def get_price(self):
		return self.__price
	def get_inventory(self):
		return self.__inventory

2.3.2自定义输出
下一个需要解决的是显示类的实例。目前为止,我们已经通过get_()方法使用print()语句来显示类的实例的各个属性了。但是,如果需要在程序多次显示类的数据,这个方法就显得过时了。Python提供了一种更简单的方法来完成此事。就是为类定义__str__()辅助函数,告诉Python如何将这个类对象显示为一个字符串值。任何时候,当程序将这个类实例作为一个字符串引用时(在print()语句中使用它时),Python会调用类的定义中__str__()方法。让__str__()方法返回字符串值,他将类的属性格式化为一个字符串。如下:

class Product:
	def __init__(self,description = ’new',price = 0,inventory = 0):
		self.__description = description
		self.__price = price
		self.__inventory = inventory
	def __str__(self):
		return  ' {0} - price : ${1:.2f}, inventory: {2:d}'.format(self.__description , self.get__price ,self.__inventory)  

要显示这个类的实例的属性值,只需要在print()语句中引用这个实例变量就可以了。

prod6 = Product ( 'banana ' ,1.50, 30)
print(prod6)

输出:

banana - price :  $1.50 ,inventory :  30
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值