美文网首页
9.4.6 在一个模块中导入另一个模块

9.4.6 在一个模块中导入另一个模块

作者: python大大 | 来源:发表于2017-10-15 23:45 被阅读0次

    需要将类分散到多个模块中,以免模块太大,或在同一个模块中存储不相关的类。将类存储在多个模块中时,你可能会发现一个模块中的类依赖于另一个模块中的类。
    在这种情况下,可在前一个模块中导入必要的类

    屏幕快照 2017-10-15 下午11.43.52.png

    electric_car.py

    """A set of classes that can be used to represent electric cars."""
    
    from car import Car
    
    class Battery():
        """A simple attempt to model a battery for an electric car."""
    
        def __init__(self, battery_size=60):
            """Initialize the batteery's attributes."""
            self.battery_size = battery_size
    
        def describe_battery(self):
            """Print a statement describing the battery size."""
            print("This car has a " + str(self.battery_size) + "-kWh battery.")  
            
        def get_range(self):
            """Print a statement about the range this battery provides."""
            if self.battery_size == 60:
                range = 140
            elif self.battery_size == 85:
                range = 185
                
            message = "This car can go approximately " + str(range)
            message += " miles on a full charge."
            print(message)
        
            
    class ElectricCar(Car):
        """Models aspects of a car, specific to electric vehicles."""
    
        def __init__(self, manufacturer, model, year):
            """
            Initialize attributes of the parent class.
            Then initialize attributes specific to an electric car.
            """
            super(ElectricCar,self).__init__(manufacturer, model, year)
            self.battery = Battery()
    
    

    car.py

    """A class that can be used to represent a car."""
    
    class Car(object):
        """A simple attempt to represent a car."""
    
        def __init__(self, manufacturer, model, year):
            """Initialize attributes to describe a car."""
            self.manufacturer = manufacturer
            self.model = model
            self.year = year
            self.odometer_reading = 0
            
        def get_descriptive_name(self):
            """Return a neatly formatted descriptive name."""
            long_name = str(self.year) + ' ' + self.manufacturer + ' ' + self.model
            return long_name.title()
        
        def read_odometer(self):
            """Print a statement showing the car's mileage."""
            print("This car has " + str(self.odometer_reading) + " miles on it.")
            
        def update_odometer(self, mileage):
            """
            Set the odometer reading to the given value.
            Reject the change if it attempts to roll the odometer back.
            """
            if mileage >= self.odometer_reading:
                self.odometer_reading = mileage
            else:
                print("You can't roll back an odometer!")
        
        def increment_odometer(self, miles):
            """Add the given amount to the odometer reading."""
            self.odometer_reading += miles
    
    
    

    my_cars.py

    from car import Car
    from electric_car import ElectricCar
    
    my_beetle = Car('volkswagen', 'beetle', 2015)
    print(my_beetle.get_descriptive_name())
    
    my_tesla = ElectricCar('tesla', 'roadster', 2015)
    print(my_tesla.get_descriptive_name())
    
    

    相关文章

      网友评论

          本文标题:9.4.6 在一个模块中导入另一个模块

          本文链接:https://www.haomeiwen.com/subject/hwehuxtx.html