Python 学习笔记16 类 - 导入

我们在编码的过程中,可能会给对象添加越来越多的功能,即使我们使用了继承,也不可避免的使文件越来越臃肿。

为了避免这种情况, Python允许将对象存储在模块中,并且可以在其他模块中进行导入。

其实这和C#中的命名空间相类似。

我们首先准备了一个叫car.py的模块,其中包含了多个对象:

class Car():

    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year
        self.odometer_reading = 0

    def get_description_name(self):
        long_name = str(self.year) + ' ' + self.make + ' ' + self.model
        return long_name.title()

    def read_odometer(self):
        print("This car has " + str(self.odometer_reading) + " miles on it.")

    def update_odometer(self, mileage):
        if mileage >= self.odometer_reading:
            self.odometer_reading = mileage
        else:
            print("You cannot do that.")

    def increase_odometer(self, miles):
        if miles >= 0:
            self.odometer_reading += miles
        else:
            print("The value is invalid, please input the number which should more than zero.")

    def fill_gas(self):
        print("Car is filling gas.")


'''生成一个电池类'''
class Battery():
    def __init__(self, size = 100):
        self.size = size

    def describe_battery(self):
        print("Battery has " + str(self.size) + "-kwh battery. " )

    def show_range(self):
        print("Battery has " + str(self.size * 3) + " killmaters on full charge")

'''继承car,生成一个新类'''
class ElectricCar(Car):
    def __init__(self, make, model, year):
        super().__init__(make, model, year)
        self.battery = Battery()

    def fill_gas(self):
        print("Electric car no gas tank.")

 

接下来我们新建一个my_car的模块,并导入Car类并创建实例,并且调用一些方法:

#-*- coding:utf-8 -*-
from car import Car

my_new_car = Car('Volvo', 'V60', '2020')
print(my_new_car.get_description_name())

my_new_car.odometer_reading = 200
my_new_car.read_odometer()


'''
输出:
2020 Volvo V60
This car has 200 miles on it.

'''

 

同样,我们也可以在一个模块里面存储、导入多个类,进行操作:

#-*- coding:utf-8 -*-
from car import Car, ElectricCar

my_new_car = Car('Volvo', 'V60', '2020')
print(my_new_car.get_description_name())

my_new_car.odometer_reading = 200
my_new_car.read_odometer()

my_byd_tang = ElectricCar('BYD', 'Tang', '2020')
my_byd_tang.battery.show_range()
my_byd_tang.fill_gas()
'''
输出:
2020 Volvo V60
This car has 200 miles on it.
Battery has 300 killmaters on full charge
Electric car no gas tank.

'''

 

当然你可以导入模块中的所有的类:

#-*- coding:utf-8 -*-
from car import *

my_new_car = Car('Volvo', 'V60', '2020')
print(my_new_car.get_description_name())

my_new_car.odometer_reading = 200
my_new_car.read_odometer()

my_byd_tang = ElectricCar('BYD', 'Tang', '2020')
my_byd_tang.battery.show_range()
my_byd_tang.fill_gas()

my_battery = Battery()
my_battery.show_range()
'''
输出:
2020 Volvo V60
This car has 200 miles on it.
Battery has 300 killmaters on full charge
Electric car no gas tank.
Battery has 300 killmaters on full charge
'''

 

也可以直接导入整个模块,不过这种方式需要在类的名字前加上模块名:

#-*- coding:utf-8 -*-
import car


my_new_car = car.Car('Volvo', 'V60', '2020')
print(my_new_car.get_description_name())

my_new_car.odometer_reading = 200
my_new_car.read_odometer()

my_byd_tang = car.ElectricCar('BYD', 'Tang', '2020')
my_byd_tang.battery.show_range()
my_byd_tang.fill_gas()

my_battery = car.Battery()
my_battery.show_range()
'''
输出:
2020 Volvo V60
This car has 200 miles on it.
Battery has 300 killmaters on full charge
Electric car no gas tank.
Battery has 300 killmaters on full charge
'''

 

我们在实际的编码过程中,还是比较推荐一个类只定义在单一的模块中,这样模块的大小不会太大,也方便其他模块进行调用。

也方便进行维护。

 

posted @ 2019-07-21 14:11  追风狂想  阅读(176)  评论(0编辑  收藏  举报