python入门_day7_chap9_类(下)

9.4导入类

1.导入单个类
我们将这样解决这个命名问题:将Car 类存储在一个名为car.py的模块中,该模块将覆盖前面使用的文件car.py。从现在开始,使用该模块的程序都必须使用更具体的文件 名,如my_car.py。下面是模块car.py,其中只包含Car 类的代码:

car.py

"""一个可以用于表示汽车的类"""
class Car():

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

    def get_descriptive_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.odometr_reading:
            self.odometer_reading=mileage
        else:
            print('You cant roll back an odometer')


    def increment_odometer(self,miles):
        self.odometer_reading+=miles

下面来创建另一个文件——my_car.py,在其中导入Car 类并创建其实例:

from car 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()

import 语句让Python打开模块car,并导入其中的Car 类,这样我们就可以使用Car 类。

2.在一个模块中存储多个类

car.py
包含了Car,ElectricCar,Battery三个类。

class Car():

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

    def get_descriptive_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.odometr_reading:
            self.odometer_reading=mileage
        else:
            print('You cant roll back an odometer')


    def increment_odometer(self,miles):
        self.odometer_reading+=miles
        

    def fill_gas_tank(self):
        print('The gas tank has '+str(self.gas)+'-l gas in it')


class Battery():

    def __init__(self,battery_size=70):
        self.battery_size=battery_size


    def describe_battery(self):
        print('This car has a '+str(self.battery_size)+'-kwh battery.')


    def get_range(self):
        if self.battery_size==70:
            range=240
        elif self.battery_size==85:
            range=270

        message='This can can go approximately '+str(range)+' km .'
        print(message)

    def upgrade_battery(self):
        if self.battery_size!=85:
            self.battery_size=85
        


class ElectricCar(Car):

    def __init__(self,make,model,year):
        #初始化父类的属性
        super().__init__(make,model,year)
        self.battery=Battery()
        

    def describe_battery(self):
        print('This car has a '+str(self.battery_size)+'-kwh battery')

    def fill_gas_tank(self):
        print('Do not need a gas tank')

my_electric_car.py

from car import ElectricCar

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

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

输出与我们前面看到的相同,但大部分逻辑都隐藏在一个模块中:
在这里插入图片描述

3.从一个模块中导入多个类

from car 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())

在这里插入图片描述

4.导入整个模块

还可以导入整个模块,再使用句点表示法访问需要的类。

import car

my_beetle = car.Car('volkswagen', 'beetle', 2016)
print(my_beetle.get_descriptive_name())

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

5.导入模块中所有类

语法: from module_name import *

6.在一个模块中导入另一个模块

有时候,需要将类分散到多个模块中,以免模块太大,或在同一个模块中存储不相关的类。将类存储在多个模块中时,你可能会发现一个模块中的类依赖于另一个模块中的类。

在这种情况下,可在前一个模块中导入必要的类。 例如,下面将Car 类存储在一个模块中,并将ElectricCar 和Battery 类存储在另一个模块中。

我们将第二个模块命名为electric_car.py (这将覆盖前面创建的文件 electric_car.py),并将Battery 和ElectricCar 类复制到这个模块中:

electric_car.py模块

 from car import Car 
 
class Battery():     
    --snip-  
class ElectricCar(Car):     
    --snip-

car.py模块

class Car():    
    --snip-

现在可以分别从两个模块中导入类,以根据需要创造不同类型的汽车。

my_cars.py

 from car import Car  
 from electric_car 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())

练习
1.导入Restaurant 类 :将最新的Restaurant 类存储在一个模块中。在另一个文件中,导入Restaurant 类,创建一个Restaurant 实例,并调 用Restaurant 的一个方法,以确认import 语句正确无误。
restaurant.py

class Restaurant():
    def __init__(self,name,cuisine_type):
        self.name=name
        self.cuisine_type=cuisine_type
        number_served=0

    def describe_restaurant(self):
        print("The restaurant's name is "+self.name.title()+'.')
        print("The cuisine_type of this restaurant is "+self.cuisine_type)

    def open_restaurant(self):
        print(self.name.title()+' is open')

    def set_number_served(self,number):
        self.number_served=number
        print('就餐人数:'+str(self.number_served))

    def increment_number_served(self,increment):
        self.number_served+=increment
        print('新增后的就餐人数:'+str(self.number_served))

restaurant_test.py

from restaurant import Restaurant

my_restaurant=Restaurant('honglajiao','sichuan food')
my_restaurant.describe_restaurant()

运行结果:
在这里插入图片描述

2 . 导入Admin 类:以为完成练习9-8而做的工作为基础,将User 、Privileges 和Admin 类存储在一个模块中,再创建一个文件,在其中创建一个Admin 实例 并对其调用方法show_privileges() ,以确认一切都能正确地运行。

user.py

class User():
    def __init__(self,first_name,last_name,location):
        self.first_name=first_name
        self.last_name=last_name
        self.location=location
        self.login_attempts=0

    def describe_user(self):
        name=self.first_name+self.last_name
        print('hello,'+name)
        print('Hope u hava a goog time in '+self.location)

    def increment_login_attempts(self):
        self.login_attempts+=1
        print('用户尝试登陆次数:'+str(self.login_attempts))

    def reset_login_attempts(self):
        self.login_attempts=0
        print('重置用户尝试登陆次数:'+str(self.login_attempts))


class Privileges():
    def __init__(self):
        self.privileges=['can add post','can delete post','can ban user']
    def show_privileges(self):
        for privilege in self.privileges:
            print('Admins '+privilege)

class Admin(User):

    def __init__(self,first_name,last_name,location):
        super().__init__(first_name,last_name,location)
        self.privileges=Privileges()

user_test.py

from users import Admin

my_admin=Admin('ming','jing','home')
my_admin.privileges.show_privileges()

运行结果:
在这里插入图片描述
3. 多个模块 :将User 类存储在一个模块中,并将Privileges 和Admin 类存储在另一个模块中。再创建一个文件,在其中创建一个Admin 实例,并对其调用方 法show_privileges() ,以确认一切都依然能够正确地运行。

第一个模块 users.py

class User():
    def __init__(self,first_name,last_name,location):
        self.first_name=first_name
        self.last_name=last_name
        self.location=location
        self.login_attempts=0

    def describe_user(self):
        name=self.first_name+self.last_name
        print('hello,'+name)
        print('Hope u hava a goog time in '+self.location)

    def increment_login_attempts(self):
        self.login_attempts+=1
        print('用户尝试登陆次数:'+str(self.login_attempts))

    def reset_login_attempts(self):
        self.login_attempts=0
        print('重置用户尝试登陆次数:'+str(self.login_attempts))

第二个模块 admin.py中导入User

from users import User

class Privileges():
    def __init__(self):
        self.privileges=['can add post','can delete post','can ban user']
    def show_privileges(self):
        for privilege in self.privileges:
            print('Admins '+privilege)

class Admin(User):

    def __init__(self,first_name,last_name,location):
        super().__init__(first_name,last_name,location)
        self.privileges=Privileges()

admin_test.py

from users import User
from admin import Admin

my_admin=Admin('ming','jing','home')
my_admin.privileges.show_privileges()

结果同上。

9.5 Python标准库

Python标准库 是一组模块,安装的Python都包含它。
可使用标准库中的任何函数和类,为此 只需在程序开头包含一条简单的import 语句。

下面来看模块collections 中的一个类——OrderedDict 。

字典让你能够将信息关联起来,但它们不记录你添加键—值对的顺序。

要创建字典并记录其中的键—值对的添加顺序,可使用模块collections 中的OrderedDict 类。OrderedDict 实例的行为几乎与字典相同,区别只在于记录了键—值对的添加顺序。

"""从模块collections 中导入了OrderedDict 类"""
from collections import OrderedDict
"""我们创建了OrderedDict 类的一个实例,并将其存储到favorite_languages 中."""
favorite_language=OrderedDict()

favorite_language['jent']='python'
favorite_language['ian']='java'
favorite_language['fiona']='c'
favorite_language['vin']='c++'
favorite_language['carl']='python'



for name,language in favorite_language.items():
    print(name.title()+"'s favorite language is "+language.title()+'.')

运行结果:
在这里插入图片描述

练习
模块random 包含以各种方式生成随机数的函数,其中的randint() 返回一个位于指定范围内的整数,例如,下面的代码返回一个1~6内的整数:
from random import randint
x = randint(1, 6)
请创建一个Die 类,它包含一个名为sides 的属性,该属性的默认值为6。编写一个名为roll_die() 的方法,它打印位于1和骰子面数之间的随机数。创建一个6面 的骰子,再掷10次。 创建一个10面的骰子和一个20面的骰子,并将它们都掷10次。

from random import randint


class Die():
    def __init__(self):
        self.sides=6

    def roll_die(self):
        self.sides=randint(1,6)
        print('六面骰子: '+str(self.sides))

    def roll_ten_die(self):
        self.sides=randint(1,10)
        print('十面骰子: '+str(self.sides))

    def roll_tewenty_die(self):
        self.sides=randint(1,20)
        print('二十面骰子: '+str(self.sides))

my_die=Die()
x=1
while x<=10:
    x+=1
    my_die.roll_die()
    my_die.roll_ten_die()
    my_die.roll_tewenty_die()

运行结果:
在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值