1 基本介绍
面向对象变成是最有效的软件编程方法之一,基于类创建对象称为实列化,从而让你可以使用类的实列。
(1)Python中约定,首字母大写的名称指的是类,后面紧接着是冒号;
(2)类中的函数称为方法__init__();开头和末尾两个下划线,这是一种约定,避免与Python默认的方法冲突;函数中self必不可少,且必须位于其它形参前面;
(3)类(面向对象)包括两个基本内容:
??????????????? 属性:对象的属性
??????????????? 方法:对象可以干什么
推荐统一的编码风格,摘自《Python编程:从入门到实践》:
(1) 类名采用驼峰命名法,即每个单词的首字母大写(如MyCarClass),实例名和模块名都采用小写,并在单词之间加下划线(如my_car_class);
(2) 每个类,都应紧跟在类定义后面包含一个文档字符串,这些字符串将在调用类的时候在跟随鼠标显示类的作用;
(3) 在类中,可使用一个空行来分隔方法;而在模块中,可使用两个空行来分隔类;
(4) 需要同时导入标准库中的模块和你编写的模块时,先编写导入标准库模块的 import 语句,再添加一个空行,然后编写导入你自己编写的模块的 import 语句
2 类实列
test_class.py:
class Car:#建立类
def __init__(self,number,color,maxvehicalspeed,maxenginespeed):
'''初始化每个车号的属性'''
self.number=number
self.color=color
self.maxvehicalspeed=maxvehicalspeed
self.maxenginespeed=maxenginespeed
def ReportMaxVehicalSpeed(self):
'''定义方法,报告最大车速'''
print(f"Number {self.number}, max vehical speed is {self.maxvehicalspeed}.")
def ReportMaxEngineSpeed(self):
'''定义方法,报告最高转速'''
print(f"Number {self.number}, max engine speed is {self.maxenginespeed}.")
#创建一个对象my_car
my_car1=Car('1001','white','180kph','5500rpm')
my_car2=Car('1002','red','220kph','6500rpm')
#调用对象的方法
my_car1.ReportMaxVehicalSpeed()
my_car1.ReportMaxEngineSpeed()
my_car2.ReportMaxVehicalSpeed()
my_car2.ReportMaxEngineSpeed()
运行结果:
Number 1001, max vehical speed is 180kph. Number 1001, max engine speed is 5500rpm. Number 1002, max vehical speed is 220kph. Number 1002, max engine speed is 6500rpm.
也可以通过编写方法increase_odometer,修改类中的值:
class Car:#建立类
def __init__(self,number,color,maxvehicalspeed,maxenginespeed):
'''初始化每个车号的属性'''
self.number=number
self.color=color
self.maxvehicalspeed=maxvehicalspeed
self.maxenginespeed=maxenginespeed
self.odometer=0#里程为0
def ReportMaxVehicalSpeed(self):
'''定义方法,报告最大车速'''
print(f"Number {self.number}, max vehical speed is {self.maxvehicalspeed}.")
def ReportMaxEngineSpeed(self):
'''定义方法,报告最高转速'''
print(f"Number {self.number}, max engine speed is {self.maxenginespeed}.")
def ReportOdometer(self):
'''报告车辆里程'''
print(f"Number {self.number}, odometer is {self.odometer}.")
def increase_odometer(self,increaseodometer):
self.odometer += increaseodometer#增加里程
#创建一个对象my_car
my_car1=Car('1001','white','180kph','5500rpm')
my_car2=Car('1002','red','220kph','6500rpm')
#调用对象的方法
my_car1.odometer=100;#初始化里程为100公里
my_car1.ReportOdometer()
my_car1.increase_odometer(10000)#增加10000公里行驶里程
my_car1.ReportOdometer()
运行结果:
Number 1001, odometer is 100. Number 1001, odometer is 10100.
3 类继承
编写类时,可以从已有的类中继承,这将自动复制已有类的所有属性和方法,原有的类称为父类,新类称为子类。
class Car:#建立父类
def __init__(self,number,color,maxvehicalspeed,maxenginespeed):
'''初始化每个车号的属性'''
self.number=number
self.color=color
self.maxvehicalspeed=maxvehicalspeed
self.maxenginespeed=maxenginespeed
self.odometer=0#里程为0
def ReportMaxVehicalSpeed(self):
'''定义方法,报告最大车速'''
print(f"Number {self.number}, max vehical speed is {self.maxvehicalspeed}.")
def ReportMaxEngineSpeed(self):
'''定义方法,报告最高转速'''
print(f"Number {self.number}, max engine speed is {self.maxenginespeed}.")
def ReportOdometer(self):
'''报告车辆里程'''
print(f"Number {self.number}, odometer is {self.odometer}km.")
def increase_odometer(self,increaseodometer):
self.odometer += increaseodometer#增加里程
class ElectricCar(Car):#建立子类
def __init__(self,number,color,maxvehicalspeed,maxenginespeed):
#通过super()初始化父类的属性
super().__init__(number,color,maxvehicalspeed,maxenginespeed)
#初始化子类的新增特有的属性,赋默认值75A
self.battery="75A"
def ReportBattery(self):#子类新增方法
#报告电池信息
print(f"Number {self.number}, Battery is {self.battery}.")
#调用
my_Elecar1=ElectricCar('2001','white','180kph','5500rpm')
my_Elecar1.odometer=10000#初始化里程为100公里
my_Elecar1.increase_odometer(10000)#增加10000公里行驶里程
my_Elecar1.ReportOdometer()#上传行驶里程
my_Elecar1.battery='65A'
my_Elecar1.ReportBattery()
运行结果:
Number 2001, odometer is 20000km. Number 2001, Battery is 65A.
如果父类的方法对子类不适应可以重写此方法,比如对ReportMaxEngineSpeed方法进行重写:
class ElectricCar(Car):#建立子类
def __init__(self,number,color,maxvehicalspeed,maxenginespeed):
#通过super()初始化父类的属性
super().__init__(number,color,maxvehicalspeed,maxenginespeed)
#初始化子类的新增特有的属性,赋默认值75A
self.battery="75A"
def ReportBattery(self):#子类新增方法
#报告电池信息
print(f"Number {self.number}, Battery is {self.battery}.")
def ReportMaxEngineSpeed(self):
'''重新定义方法,报告最高转速'''
print(f"Elecar Number {self.number}, max engine speed is {self.maxenginespeed}.")
#调用
my_Elecar1=ElectricCar('2001','white','180kph','5500rpm')
my_Elecar1.ReportMaxEngineSpeed()
执行结果:
Elecar Number 2001, max engine speed is 5500rpm.
4 将实列(类)用作属性
??????? 继承父类Car后,如果子类中,新增的属性和方法太多,可以将属性和方法单独写一个类Battery,这个类Battery在类ElectricCar中作为属性传递。
class Car:#建立父类
def __init__(self,number,color,maxvehicalspeed,maxenginespeed):
'''初始化每个车号的属性'''
self.number=number
self.color=color
self.maxvehicalspeed=maxvehicalspeed
self.maxenginespeed=maxenginespeed
self.odometer=0#里程为0
def ReportMaxVehicalSpeed(self):
'''定义方法,报告最大车速'''
print(f"Number {self.number}, max vehical speed is {self.maxvehicalspeed}.")
def ReportMaxEngineSpeed(self):
'''定义方法,报告最高转速'''
print(f"Number {self.number}, max engine speed is {self.maxenginespeed}.")
def ReportOdometer(self):
'''报告车辆里程'''
print(f"Number {self.number}, odometer is {self.odometer}km.")
def increase_odometer(self,increaseodometer):
self.odometer += increaseodometer#增加里程
class Battery:#从新建立一个类
def __init__(self,battery_size='75A'):
self.battery_size=battery_size
def ReportBattery(self):#子类新增方法
#报告电池信息
print(f"Electric car battery is {self.battery_size}.")
class ElectricCar(Car):#建立子类
def __init__(self,number,color,maxvehicalspeed,maxenginespeed):
#通过super()初始化父类的属性
super().__init__(number,color,maxvehicalspeed,maxenginespeed)
#将类用作属性
self.battery=Battery()
def ReportMaxEngineSpeed(self):
''''重新定义方法,报告最高转速'''
print(f"Electric car Number {self.number}, max engine speed is {self.maxenginespeed}.")
#调用
my_Elecar1=ElectricCar('2001','white','180kph','5500rpm')
my_Elecar1.odometer=10000#初始化里程为100公里
my_Elecar1.increase_odometer(10000)#增加10000公里行驶里程
my_Elecar1.ReportOdometer()#上传行驶里程
my_Elecar1.battery.battery_size='65A'
my_Elecar1.battery.ReportBattery()
my_Elecar1.ReportMaxEngineSpeed()
运行结果:
Number 2001, odometer is 20000km. Electric car battery is 65A. Electric car Number 2001, max engine speed is 5500rpm.
5 导入类
随着不断给类添加功能,文件可能变得很长,这就需要单独将类存入一个py文件中,在另外一个文件中调用这个类。
方法:
导入一个类:from mode_name import Class_name
导入多个类:from mode_name import Class_name1,Class_name2
导入所有的类:from mode_name import *
使用别名导入:from mode_name import Class_name as new_Class_name
导入整个模块:import mode_name (需要用句点的表示法访问需要的类,如my_car1=car.Car(....))
class_test.py
class Car:#建立父类
def __init__(self,number,color,maxvehicalspeed,maxenginespeed):
'''初始化每个车号的属性'''
self.number=number
self.color=color
self.maxvehicalspeed=maxvehicalspeed
self.maxenginespeed=maxenginespeed
self.odometer=0#里程为0
def ReportMaxVehicalSpeed(self):
'''定义方法,报告最大车速'''
print(f"Number {self.number}, max vehical speed is {self.maxvehicalspeed}.")
def ReportMaxEngineSpeed(self):
'''定义方法,报告最高转速'''
print(f"Number {self.number}, max engine speed is {self.maxenginespeed}.")
def ReportOdometer(self):
'''报告车辆里程'''
print(f"Number {self.number}, odometer is {self.odometer}km.")
def increase_odometer(self,increaseodometer):
self.odometer += increaseodometer#增加里程
class Battery:#从新建立一个类
def __init__(self,battery_size='75A'):
self.battery_size=battery_size
def ReportBattery(self):#子类新增方法
#报告电池信息
print(f"Electric car battery is {self.battery_size}.")
class ElectricCar(Car):#建立子类
def __init__(self,number,color,maxvehicalspeed,maxenginespeed):
#通过super()初始化父类的属性
super().__init__(number,color,maxvehicalspeed,maxenginespeed)
#将类用作属性
self.battery=Battery()
def ReportMaxEngineSpeed(self):
''''重新定义方法,报告最高转速'''
print(f"Electric car Number {self.number}, max engine speed is {self.maxenginespeed}.")
class_import.py:
'''调用类'''
from class_test import Car,ElectricCar
#调用
print("------use class ElectricCar---------")
my_Elecar1=ElectricCar('2001','white','180kph','5500rpm')
my_Elecar1.battery.battery_size='65A'
my_Elecar1.battery.ReportBattery()
my_Elecar1.ReportMaxEngineSpeed()
print("------use class Car---------")
my_car1=Car('2001','white','180kph','5500rpm')
my_car1.ReportMaxEngineSpeed()
运行class_import.py结果:
------use class ElectricCar--------- Electric car battery is 65A. Electric car Number 2001, max engine speed is 5500rpm. ------use class Car--------- Number 2001, max engine speed is 5500rpm.
|