1、导入单个类
2、从一个模块中导入多个类
3、导入整个模块
4、导入模块中的所有类
5、在一个模块中导入另一个模块
6、Python标准库 collections 中的OrderedDict类
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 15 20:34:36 2023
@author: USUS
"""
class Car():
"""一次魔力汽车的简单尝试"""
def __init__(self,make,model,year):
"""初始化描述汽车的属性"""
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0 ## 添加一个随时间变化的属性
# 更新里程属性的方法
def update_odometer(self,mileage):
"""
将里程读数设置为指定的值
禁止任何人将里程属性往回调
只能加,不能减
"""
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print("You can't roll back an odometer!")
# 里程属性自增的方法
def increment_odometer(self,miles):
"""将里程表读数增加指定的量"""
self.odometer_reading += miles
def get_describle_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.")
class Battery():
"""
一次模拟电动汽车电瓶的简单尝试
"""
def __init__(self,battery_size = 70):
"""初始化电瓶属性"""
self.battery_size = battery_size
def describle_battery(self):
"""打印一条描述电瓶容量的消息"""
print("This car has a "+ str(self.battery_size) + "-KWh battery.")
def get_range(self):
"""打印一条描述电瓶车容量的消息"""
if self.battery_size == 70:
ranges = 240
elif self.battery_size == 85:
ranges = 270
message = "This car can go approximately " + str(ranges)
message += " miles on full charge."
print(message)
class ElectricCar(Car):
"""模拟电动车的独特之处"""
def __init__(self, make, model, year):
"""
初始化父类的属性,再初始化电动汽车特有的属性
"""
super().__init__(make, model, year)
self.battery = Battery()
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 18 18:32:07 2023
@author: USUS
"""
class Car():
"""一次魔力汽车的简单尝试"""
def __init__(self,make,model,year):
"""初始化描述汽车的属性"""
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0 ## 添加一个随时间变化的属性
# 更新里程属性的方法
def update_odometer(self,mileage):
"""
将里程读数设置为指定的值
禁止任何人将里程属性往回调
只能加,不能减
"""
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print("You can't roll back an odometer!")
# 里程属性自增的方法
def increment_odometer(self,miles):
"""将里程表读数增加指定的量"""
self.odometer_reading += miles
def get_describle_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.")
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 18 18:32:45 2023
@author: USUS
"""
class Battery():
"""
一次模拟电动汽车电瓶的简单尝试
"""
def __init__(self,battery_size = 70):
"""初始化电瓶属性"""
self.battery_size = battery_size
def describle_battery(self):
"""打印一条描述电瓶容量的消息"""
print("This car has a "+ str(self.battery_size) + "-KWh battery.")
def get_range(self):
"""打印一条描述电瓶车容量的消息"""
if self.battery_size == 70:
ranges = 240
elif self.battery_size == 85:
ranges = 270
message = "This car can go approximately " + str(ranges)
message += " miles on full charge."
print(message)
from car1 import Car #从car1模板中导入Car这个类,需要先声明否则会报错,也可在最开头导入,相当于C++中,函数的先声明一样
class ElectricCar(Car):
"""模拟电动车的独特之处"""
def __init__(self, make, model, year):
"""
初始化父类的属性,再初始化电动汽车特有的属性
"""
super().__init__(make, model, year)
self.battery = Battery()
from car import Car # 从car类模板中导入Car类
my_new_car = Car('audi','a4',2016) # 实例化对象
print(my_new_car.get_describle_name()) # 调用对象的方法
my_new_car.odometer_reading = 23 # 更改对象的属性
my_new_car.read_odometer() # 调用对象的方法
结果:
2016 Audi A4
This car has 23 miles on it.
## 9-4-2 在一个模块中导入多个类
from car import Car,ElectricCar # 从car类模板中导入Car,ElectricCar两个类
my_bettle = Car('volkswagen', 'beetle', 2016) # 创建对象,叫做实例化
print(my_bettle.get_describle_name()) # 调用对象的方法
my_tesla = ElectricCar('tesla', 'roadster', 2016) # 创建对象,叫做实例化
print(my_tesla.get_describle_name()) # 调用对象的方法
结果:
2016 Volkswagen Beetle
2016 Tesla Roadster
import car # 导入car类模板中的所有类
my_bettle = car.Car('volkswagen', 'beetle', 2016) # 创建对象
print(my_bettle.get_describle_name()) # 调用对象方法
my_tesla = car.ElectricCar('tesla', 'roadster', 2016) # 创建对象
print(my_tesla.get_describle_name()) # 调用对象方法
结果:
2016 Volkswagen Beetle
2016 Tesla Roadster
from car import * # 从car类模板中导入所有类
my_bettle = Car('volkswagen', 'beetle', 2016) # 创建对象,实例化
print(my_bettle.get_describle_name()) # 调用对象方法
my_tesla = ElectricCar('tesla', 'roadster', 2016) # 创建对象,实例化
print(my_tesla.get_describle_name()) # 调用对象方法
结果:
2016 Volkswagen Beetle
2016 Tesla Roadster
from car1 import Car # 先导入Car父类模块
from electric_car1 import ElectricCar # 在导入子类模块
my_bettle = Car('volkswagen', 'beetle', 2016)
print(my_bettle.get_describle_name())
my_tesla = ElectricCar('tesla', 'roadster', 2016)
print(my_tesla.get_describle_name())
结果:
2016 Volkswagen Beetle
2016 Tesla Roadster
from collections import OrderedDict
favorite_languages = OrderedDict()
favorite_languages['jen'] = 'python'
favorite_languages['sarah'] = 'c'
favorite_languages['edward'] = 'ruby'
favorite_languages['phil'] = 'python'
for name,language in favorite_languages.items():
print(name.title() + "'s favorite language is " +
language.title())
结果:
Jen's favorite language is Python
Sarah's favorite language is C
Edward's favorite language is Ruby
Phil's favorite language is Python