示例#1
0
from cars import ElectricCar

my_tesla = ElectricCar('tesla', 'model s', 2019)
print(my_tesla.description())
my_tesla.battery.decribe_battery()
my_tesla.battery.get_range()
示例#2
0
#导入类
from cars import Car

my_new_car = Car('audi','a4','2016')
print(my_new_car.get_descriptive_name())

my_new_car.odometer_reading = 23
my_new_car.read_odometer()
#导入多个类
from cars import ElectricCar,Car

my_tesla = ElectricCar('tesla','model s',2016)

print(my_tesla.get_descriptive_name())
my_tesla.battery.describe_battery()
my_tesla.battery.get_range()

#导入一个模块
import cars
my_beetle =cars.Car('volkswagen','beetle',2016)
print(my_beetle.get_descriptive_name())

my_tesla = cars.Car('tesla','roadster',2016)


示例#3
0
# Importing multiple classes from a module
# We can import as many classes as we need into a program file

# We import multiple classes from a module by separating each class with a comma
# Once imported, we are free to use each class as needed
from cars import Car, ElectricCar

myBeetle = Car("volkswagen", "beetle", 2019)
print(myBeetle.getDescriptiveName())

myTesla = ElectricCar("tesla", "roadster", 2019)
print(myTesla.getDescriptiveName())

# Importing an entire module
# We can also import an entire module and then access the classes we need by using dot notation. This approach is simple and results in code that is easy to read.
print("\n")
# Here we import the entire cars module and we access it by using moduleName.ClassName syntax
import cars

myBeetle = cars.Car("volkswagen", "beetle", 2019)
print(myBeetle.getDescriptiveName())

myTesla = cars.ElectricCar("tesla", "roadster", 2019)
print(myTesla.getDescriptiveName())

# Importing all classes from a module
# from moduleName import *

# This method is not recommended for a few reasons.
# One with this approach it's unclear which classes we're using from the module. This results in confusion with names in the file. It can result in importing another module that has the same naming for classes which will conflict and or overwrite your classes. This can lead to difficult dianoses.
示例#4
0
#Chapter 9 ex 9 date : 02/07/17.

#Imports
from cars import ElectricCar,Battery

#Variables

#Objects 
my_car = ElectricCar("Tesla","Roadster",2008,Battery())
my_battery = my_car.battery

#Functions

#Body
my_battery.get_range()
my_battery.upgrade_battery()
my_battery.get_range()
示例#5
0
# Storing multiple classes in a module
# We can store as many classes as we need in a single module, although each class in a module should be related somehow.
# We added the classes Battery and ElectricCar to cars.py as they are related

from cars import ElectricCar

# As if the class was in the file, once we import it, we can use it as normal
myTesla = ElectricCar("tesla", "model s", 2019)

print(myTesla.getDescriptiveName())
myTesla.battery.describeBattery()
myTesla.battery.getRange()
示例#6
0
from cars import Car, ElectricCar

my_gti = Car('volkswagen', 'gti', '2018')
print(my_gti.get_descriptive_name())

my_gti.odometer_reading = 16
my_gti.read_odometer()

my_tesla = ElectricCar('tesla', 'roadster', 2016)
print(my_tesla.get_descriptive_name())

my_tesla.battery.describe_battery()

from cars import ElectricCar

my_tesla = ElectricCar("tesla", "S-type", 2018)
my_tesla.battery.describe_battery()
my_tesla.battery.get_range()
my_tesla.battery.upgrade_battery()
my_tesla.battery.get_range()
my_tesla.battery.describe_battery()