有时候我们给类添加的内容很长,是的文件显得冗长,我们就可以单独来一个文件放我们写好的类,在主文件中去导入这个模板的类然后在主文件中使用
导入的格式:from 文件名 import 类名
#类模板,文件名是carClassTemplate.py class Car(): def __init__(self,make,model,year): self.make = make self.model = model self.year = year self.odometer_read = 0 def get_descriptive_name(self): long_name = str(self.year)+" "+ self.make + " " + self.model return long_name def read_odometer(self): print("the car's odometer is " + self.odometer_read + " !") def update_odometer(self,mlies): self.odometer_read = mlies def increament_odometer(self,increament_mlies): self.odometer_read += increament_mlies def read_odometer(self): print("the car's odometrs is "+ str(self.odometer_read) + " !") #主文件 #使用导入类(从文件carClassTemplate.py中导入了一个类Car) from carClassTemplate import Car my_new_car = Car('audi','A6','2020') my_new_car.update_odometer(200) my_new_car.read_odometer() #the car's odometrs is 200 ! my_new_car.increament_odometer(98) my_new_car.read_odometer() #the car's odometrs is 298 !
可以同时导入多个类(下面把Battery类的实例作为my_car类的属性为例):
#类模板,文件名是carClassTemplate.py class Car(): def __init__(self,make,model,year): self.make = make self.model = model self.year = year self.odometer_read = 0 def get_descriptive_name(self): long_name = str(self.year)+" "+ self.make + " " + self.model return long_name def read_odometer(self): print("the car's odometer is " + self.odometer_read + " !") def update_odometer(self,mlies): self.odometer_read = mlies def increament_odometer(self,increament_mlies): self.odometer_read += increament_mlies def read_odometer(self): print("the car's odometrs is "+ str(self.odometer_read) + " !") class Battery(): def __init__(self): self.battery_size = 70 def update_battery_size(self,new_battery_size): self.battery_size = new_battery_size def read_battery_size(self): print("the car's batter size is " + str(self.battery_size) + " !") #主文件 #使用导入类(从文件carClassTemplate.py中导入了类Car,类Battery from carClassTemplate import Car , Battery class my_car(Car): def __init__(self,make,model,year): super(my_car,self).__init__(make,model,year) self.battery_size = Battery() my_new_car = my_car('audi','A6',2020) car_info = my_new_car.get_descriptive_name() print(car_info) #2020 audi A6 my_new_car.battery_size.read_battery_size() #the car's batter size is 70 ! my_new_car.battery_size.update_battery_size(90) my_new_car.battery_size.read_battery_size() #the car's batter size is 90 ! my_new_car.update_odometer(90) my_new_car.read_odometer() #the car's odometrs is 90 ! my_new_car.update_odometer(200) my_new_car.read_odometer() #the car's odometrs is 200 ! my_new_car.increament_odometer(90) my_new_car.read_odometer() #the car's odometrs is 290 !
也可以导入整个模板,但是这时候就需要使用点来读取具体的类名称
import carClassTemplate #导入整个模板 class my_car(carClassTemplate.Car): def __init__(self,make,model,year): super(my_car,self).__init__(make,model,year) self.battery_size = carClassTemplate.Battery() my_new_car = my_car('audi','A6',2020) car_info = my_new_car.get_descriptive_name() print(car_info) #2020 audi A6 my_new_car.battery_size.read_battery_size() #the car's batter size is 70 ! my_new_car.battery_size.update_battery_size(90) my_new_car.battery_size.read_battery_size() #the car's batter size is 90 ! my_new_car.update_odometer(90) my_new_car.read_odometer() #the car's odometrs is 90 ! my_new_car.update_odometer(200) my_new_car.read_odometer() #the car's odometrs is 200 ! my_new_car.increament_odometer(90) my_new_car.read_odometer() #the car's odometrs is 290 !
from carClassTemplate import * #导入所有模板的类
上边是导入所有的类模板中的类,不推荐这种写法,原因有没有明确指出用到了哪些模板中类,这在接下来使用其他类的时候会有起名称有冲突的风险