# 9.4 导入类 # 9.4.1 导入单个类 from car1 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() # 9.4.3 从一个模块中导入多个类 from car1 import Car, ElectricCar my_beetle = Car('volkswagen', 'beetle', 2016) print(my_beetle.get_descriptive_name()) my_tesla = ElectricCar('tesla', 'roadster', 2016) print(my_tesla.get_descriptive_name()) # 9.4.4 导入整个模块 import car1 my_beetle = car1.Car('volkswagen', 'beetle', 2016) print(my_beetle.get_descriptive_name()) my_tesla = car1.ElectricCar('tesla', 'roadster', 2016) print(my_tesla.get_descriptive_name())
def test(): aCar = Car() aCar.connect('notify', myCallback) aCar.set_property('fuel', 5.0)
# car1main.py from car1 import Car toyota = Car("Toyota", 2005) print(toyota.make) print(toyota.year) print(toyota) toyota.year = 2017 honda = Car("Honda", 2010) print("Cars: {}: {}, {}: {}".format(toyota.make, toyota.year, honda.make, honda.year)) print("Cars: {0.make}: {0.year}, {1.make}, {1.year}".format(toyota, honda)) print(toyota.start) toyota.start_engine() # same as #Car.start_engine(toyota) # There is a bug in start_engine print(toyota) print(Car.power_source) print(toyota.power_source) print(honda.power_source) toyota.horse_power = 220 # define new attribute print("Toyota.horse_power: {} HP".format(toyota.horse_power)) #print(honda.horse_power) # Error, why? print("Switch all car power to gas:")
# car1main.py from car1 import Car toyota = Car("Toyota", 2005) print(toyota.make) print(toyota.year) print(toyota) lexus = Car("Lexus") print(lexus) toyota.year = 2017 lexus.year = 2019 print(lexus) honda = Car("Honda", 2010) # 2 ways to print data attributes print("Cars: {}: {}, {}: {}".format(toyota.make, toyota.year, honda.make, honda.year)) print("Cars: {0.make}: {0.year}, {1.make}, {1.year}".format(toyota, honda)) print(toyota.start) toyota.start_engine() toyota.start = True print(toyota) # same as Car.start_engine(toyota) # There is a bug in start_engine ? print(toyota) print(Car.power_source)
from car1 import Car from electric_car import ElectricCar my_car = Car("audi", "A8", 2018) # my_car = car2.Car("audi", "A8", 2018) print(my_car.get_descriptive_name()) my_car.odometer_reading = 30 my_car.read_odometer() my_new_car = ElectricCar("benz", "a700", 2019) # my_new_car = car2.ElectricCar("benz", "a700", 2019) print(my_new_car.get_descriptive_name()) my_new_car.battery.describe_battery() my_new_car.battery.get_range()
def test(): aCar = Car() aCar.connect("notify", myCallback) aCar.set_property("fuel", 5.0)
from car1 import Car from my_electric_car1 import ElectricCar my_beetle = Car('volkswagen', 'beetle', 2016) print(my_beetle.get_descriptive_name()) my_tesla = ElectricCar('tesla', 'roadster', 2016) print(my_tesla.get_descriptive_name())
def test(): aCar = Car() aCar.connect('notify::fuel', myCallback) aCar.set_property('fuel', 5.0)