700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 【面向对象编程】(1) 类实例化的基本方法

【面向对象编程】(1) 类实例化的基本方法

时间:2022-06-20 14:40:12

相关推荐

【面向对象编程】(1) 类实例化的基本方法

各位同学好,本章节和大家分享一下面向对象编程的一些方法,通过一些案例带大家由浅入深掌握面向对象的编程。

1. 最基本的类实例化

创建类的方法是class 变量名:实例化方法是类名()分配属性的方法是实例名.属性名 = 值

#(1)创建类class Item: # 创建类的方法是 class 类名:pass # 不执行任何内容#(2)实例化item1 = Item() # 创建Item类的实例化对象item1#(3)给实例化对象分配属性item1.name = 'phone'item1.price = 100item1.quantity = 5#(4)打印结果print(type(item1)) # <class '__main__.Item'>print(type(item1.name)) # <class 'str'>print(type(item1.price)) # <class 'int'>print(type(item1.quantity)) # <class 'int'>

2. 在类中创建方法

在类的内部定义的函数,我们称之为方法,它可以运用于所有的类实例化对象上。我们在类的内部定义的函数,它的第一个参数默认是self代表类实例化对象,下面代码中是指item1。在类的外部调用类的内部定义的方法对象名.方法名(参数)

#(1)定义类class Item: # 创建类的方法是 class 类名:#(2)在类中创建方法# 当调用这个方法时,将对象本身作为第一个参数self传递def calculate_total_price(self, x, y): return x * y # 返回乘积#(3)实例化item1 = Item() # 创建Item类的实例化对象item1#(4)给实例化对象分配属性item1.name = 'phone'item1.price = 100item1.quantity = 5#(5)调用方法,self代表item1,传入两个参数x代表价格,y代表数量,res = item1.calculate_total_price(item1.price, item1.quantity)print(res) # 500# 再实例化一个item2 = Item() # 创建Item类的实例化对象item1item2.name = 'MacBook'item2.price = 1000item2.quantity = 3print(item2.calculate_total_price(item2.price, item2.quantity))# 输出 3000

3. 类的初始化

3.1 初始化无默认值

在类实例化之后,需要分别给每个对象分配属性,如上一节的第(4)步。如果对象很多的话,极大增加了工作量。因此我们希望在类实例化对象的时候就能自动给对象分配属性初始化方法:在类中定义def __init__(self, 参数):在类实例化时,会自动执行初始化函数中的所有内容,自动完成属性分配。

#(1)定义类class Item: # 创建一个类#(2)初始化,实例化这个类时,会自动执行init内容def __init__(self, name, price, quantity):# 动态属性分配self.name = name # 给实例化属性赋值,相当于item1.name = 'phone'self.price = price # 相当于item1.price = 100self.quantity = quantity # 相当于item2.quantity = 3#(3)实例化,创建Item类的实例化对象item1# 自动执行初始化中的内容,不需要一个一个去定义 item1 = Item('Phone', 100, 5) item2 = Item('MacBook', 1000, 3)# 打印结果print('name1:', item1.name, 'price1:', item1.price, 'quantity1:', item1.quantity)print('name2:', item2.name, 'price2:', item2.price, 'quantity2:', item2.quantity)# name1: Phone price1: 100 quantity1: 5# name2: MacBook price2: 1000 quantity2: 3

3.2 初始化有默认值

在初始化的过程中,可以给属性设置默认值。那样的话,在实例化时,如果不给定某个属性的值,那么这个属性就被赋予默认值。如下面在初始化时,已经给定了price和quantity这两个属性的默认值,因此item1就直接使用这两个属性的默认值,item2改变这两个属性的默认值。

#(1)创建类class Item: # 创建一个类#(2)初始化,实例化这个类时,会自动执行init内容# price和quantity存在默认值,可以不用传值def __init__(self, name, price=50, quantity=10): # 动态属性分配self.name = name # 给实例化属性赋值,相当于item1.name = 'phone'self.price = price # 相当于item1.price = 100self.quantity = quantity # 相当于item2.quantity = 3#(3)实例化,创建Item类的实例化对象item1# 自动执行初始化中的内容,不需要一个一个去定义 item1 = Item('Phone') # 使用默认值的price和quantityitem2 = Item('MacBook', 1000, 3) # 不使用默认值,自己赋值# 打印结果print('name1:', item1.name, 'price1:', item1.price, 'quantity1:', item1.quantity)print('name2:', item2.name, 'price2:', item2.price, 'quantity2:', item2.quantity)# name1: Phone price1: 50 quantity1: 10# name2: MacBook price2: 1000 quantity2: 3

4. 初始化后调用类中定义的方法

接着类初始化之后,在类中定义方法,第一个参数默认是实例化对象 self,这时候不需要传入额外参数。因为经初始化之后的对象,就已经被赋予了属性及属性值,可直接通过self.属性名读取该属性值

#(1)定义类class Item: #(2)初始化,如果实例化时不给定实例化属性的值,那就使用默认值def __init__(self, name, price=100, quantity=10):# 给实例化对象分配属性self.name = nameself.price = priceself.quantity = quantity#(3)在类中定义方法,self代表实例化对象def calculate_total_price(self):# 实例化对象在初始化过程中已经附有了属性,可直接在定义的方法中计算return self.price * self.quantity#(4)创建Item类的实例化对象item1item1 = Item('Phone', 100, 5) # 自定义各个属性的值#(5)调用类中定义的方法,将item1作为对象self传入该方法res = item1.calculate_total_price()print(res) # 500# ==2== 补充# 这里在实例化对象过程中没有指定数据类型,如果我们传入的price属性的值是字符串会发生什么呢item1 = Item('Phone', '100', 5) # 传入各个属性的值res = item1.calculate_total_price() # 调用类方法计算price和quantity的乘积# 相当于'100'*5,即把字符串100打印5次print(res) # 100100100100100

可是,如果在类实例化的时候,传入的属性值的类型不符合要求,就得不到想要的结果。比如传入的price是字符串类型的'100',而quantity是整型的5,这两个不同的数据类型相乘,'100'*5表示将字符串'100'打印5次,得到的不是我们预期的结果。下面讲一下如何处理这种情况。

5. 初始化时,指定传入的数据类型

在初始化函数中指定数据类型,属性名: 数据类型,在类实例化的时候会提醒需要的什么样的数据类型。如下,name属性指定数据类型为str字符串类型,price属性为浮点类型,quantity属性已经赋予默认值,那么它的数据类型和默认值的数据类型相同

#(1)创建类class Item: #(2)初始化,self代表实例化对象# 指定name属性的值为字符串类型,price属性的值为浮点类型,# quantity属性已经附有默认值,不需要再指定类型,因为quantity的数据类型就是默认值的数据类型def __init__(self, name: str, price: float, quantity=10):# 给实例化对象分配属性self.name = nameself.price = priceself.quantity = quantity#(3)定义类方法def calculate_total_price(self):# 计算乘积return self.price * self.quantity#(4)创建Item类的实例化对象item1 = Item('Phone', 100) # 指定name属性和price属性,quantity属性使用默认值#(5)调用类中的方法res = item1.calculate_total_price()print(res) # 1000

6. 初始化时,检查输入值是否符合预期

这里用到assert方法。在上一节中,我们规定了数据类型,但如果数据类型输入对了,但是值的范围不满足要求那要怎么办呢。方法:assert 条件, 报错内容

如下面代码中的assert price >= 0, f'Price {price} is not greater or equal to 0'。意思是,如果price接收的属性值大于等于0,那就正常运行;如果price的值小于0,程序就报错,报错内容是逗号后面的内容。

#(1)创建一个类class Item: #(2)初始化,self代表实例化对象,指定实例化属性的数据类型def __init__(self, name:str, price:float, quantity=10):#(3)判断输入值是否满足要求,不满足则报错,报错内容是逗号后面的# 比如,要求每个实例的价格属性price是大于0的,满足要求则不显示逗号后面的内容assert price >= 0, f'Price {price} is not greater or equal to 0' assert quantity >= 0, f'Quantity {quantity} is not greater or equal to 0'#(4)给每个实例属性赋值self.name = nameself.price = priceself.quantity = quantity#(5)定义类方法def calculate_total_price(self):# 计算乘积return self.price * self.quantity#(6)Item类的实例化对象item1item1 = Item('Phone', -100, 3)#(7)调用类实例化方法res = item1.calculate_total_price()print(res)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。