《Python编程从入门到实践》第9章类动手试一试答案(附代码)

目录

9-1(9-2) 餐馆

9-3 用户

9-4 就餐人数

9-5 尝试登陆次数

9-6 冰淇淋小店

9-7 管理员

9-8 权限

9-10 导入Restaurant类

9-11 导入Admin类

9-12 多个模块​


9-1(9-2) 餐馆

#!/usr/bin/env python
# -*- coding:utf-8 -*-


class Restaurant():
    """模拟餐馆信息的简单尝试"""

    def __init__(self, restaurant_name, cuisine_type):
        """初始化描述汽车的属性"""
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type

    def describe_restaurant(self):
        """返回餐馆信息"""
        long_name = self.restaurant_name + ' ' + self.cuisine_type
        return long_name.title()

    def open_restaurant(self):
        """打印餐厅营业消息"""
        print("The restaurant is opening!!")


my_restaurant1 = Restaurant('美味', '炒菜')
print(my_restaurant1.describe_restaurant())
my_restaurant1.open_restaurant()

my_restaurant2 = Restaurant('香辣', '香锅')
print(my_restaurant2.describe_restaurant())
my_restaurant2.open_restaurant()

my_restaurant3 = Restaurant('欢欢', '火锅')
print(my_restaurant3.describe_restaurant())
my_restaurant3.open_restaurant()

运行结果:

《Python编程从入门到实践》第9章类动手试一试答案(附代码)_第1张图片


9-3 用户

#!/usr/bin/env python
# -*- coding:utf-8 -*-


class User():
    def __init__(self, first_name, last_name, age):
        self.first_name = first_name
        self.last_name = last_name
        self.age = age

    def describe_user(self):
        """描述用户信息"""
        long_name = self.last_name + ' ' + self.first_name + ' ' + self.age
        print(long_name.title())

    def greet_user(self):
        """向用户问好"""
        print('Hello,' + ' ' + self.last_name + ' ' + self.first_name + '!')


user = User('San', 'Zhang', '20')

user.describe_user()
user.greet_user()

运行结果:


9-4 就餐人数

 

#!/usr/bin/env python
# -*- coding:utf-8 -*-


class Restaurant():
    """模拟餐馆信息的简单尝试"""

    def __init__(self, restaurant_name, cuisine_type):
        """初始化描述餐馆的属性"""
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type
        self.number_served = 0

    def describe_restaurant(self):
        """返回餐馆信息"""
        long_name = self.restaurant_name + ' ' + self.cuisine_type
        return long_name.title()

    def open_restaurant(self):
        """打印餐厅营业消息"""
        print("The restaurant is opening!!")

    def set_number_served(self, numbers):
        """设置就餐人数"""
        self.number_served = numbers

    def increment_number_served(self, numbers):
        self.number_served += numbers

    def read_number_served(self):
        """打印一条就餐人数信息"""
        print("There are " + str(self.number_served) + ' people eating in this restaurant')


# 创建餐馆实例
my_restaurant = Restaurant('欢欢', '火锅')

# 打印餐馆信息
print(my_restaurant.describe_restaurant())
my_restaurant.open_restaurant()

# 打印初始餐馆就餐人数
my_restaurant.set_number_served(10)
my_restaurant.read_number_served()

# 打印认为的可能就餐人数
my_restaurant.increment_number_served(100)
my_restaurant.read_number_served()

运行结果:

《Python编程从入门到实践》第9章类动手试一试答案(附代码)_第2张图片


9-5 尝试登陆次数

#!/usr/bin/env python
# -*- coding:utf-8 -*-


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

    def describe_user(self):
        """描述用户信息"""
        long_name = self.last_name + ' ' + self.first_name + ' ' + self.age
        print(long_name.title())

    def greet_user(self):
        """向用户问好"""
        print('Hello,' + ' ' + self.last_name + ' ' + self.first_name + '!')

    def increment_login_attempts(self):
        """计数登录次数"""
        self.login_attempts += 1

    def reset_login_attempts(self):
        """重置登录次数为0"""
        self.login_attempts = 0


user = User('San', 'Zhang', '20')

# 打印用户信息并问好
user.describe_user()
user.greet_user()

# 多次调用increment_login_attempts
for i in range(1, 6):
    user.increment_login_attempts()
    print(user.login_attempts)

user.reset_login_attempts()
print("login_attempts重置为: " + str(user.login_attempts))

运行结果:

《Python编程从入门到实践》第9章类动手试一试答案(附代码)_第3张图片


9-6 冰淇淋小店

#!/usr/bin/env python
# -*- coding:utf-8 -*-


class Restaurant():
    """模拟餐馆信息的简单尝试"""

    def __init__(self, restaurant_name, cuisine_type):
        """初始化描述餐馆的属性"""
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type
        self.number_served = 0

    def describe_restaurant(self):
        """返回餐馆信息"""
        long_name = self.restaurant_name + ' ' + self.cuisine_type
        return long_name.title()

    def open_restaurant(self):
        """打印餐厅营业消息"""
        print("The restaurant is opening!!")

    def set_number_served(self, numbers):
        """设置就餐人数"""
        self.number_served = numbers

    def increment_number_served(self, numbers):
        self.number_served += numbers

    def read_number_served(self):
        """打印一条就餐人数信息"""
        print("There are " + str(self.number_served) + ' people eating in this restaurant')


class IceCreamStand(Restaurant):
    def __init__(self, restaurant_name, cuisine_type):
        """初始化父类的属性"""
        super().__init__(restaurant_name, cuisine_type)  # 初始化父类的属性

        self.flavors = []

    def set_flavors(self, flavors):
        """设置冰淇淋餐厅口味"""
        self.flavors = flavors

    def read_flavors(self):
        """打印餐厅冰淇淋口味"""
        print('The taste of ice cream has:', end='')
        for flavor in self.flavors:
            print(flavor, end=' ')


# 创建冰淇淋餐厅实例
my_icecreamstand = IceCreamStand('欢欢', '冰淇淋')

# 打印冰淇淋信息
print(my_icecreamstand.describe_restaurant())

# 打印冰淇淋口味
my_icecreamstand.set_flavors(['蓝莓', '巧克力', '芒果'])
my_icecreamstand.read_flavors()

运行结果:


9-7 管理员

#!/usr/bin/env python
# -*- coding:utf-8 -*-


class User():
    """用户类"""
    def __init__(self, first_name, last_name, age):
        self.first_name = first_name
        self.last_name = last_name
        self.age = age
        self.login_attempts = 0

    def describe_user(self):
        """描述用户信息"""
        long_name = self.last_name + ' ' + self.first_name + ' ' + self.age
        print(long_name.title())

    def greet_user(self):
        """向用户问好"""
        print('Hello,' + ' ' + self.last_name + ' ' + self.first_name + '!')

    def increment_login_attempts(self):
        """计数登录次数"""
        self.login_attempts += 1

    def reset_login_attempts(self):
        """重置登录次数为0"""
        self.login_attempts = 0


class Admin(User):
    """特殊用户Admin"""

    """初始化父类属性"""
    def __init__(self, first_name, last_name, age):
        super().__init__(first_name, last_name, age)  # 初始化父类属性

        self.privileges = ['can add post', 'can delete post', 'can ban user']  # Admin类特有的属性

    def show_privileges(self):
        print('管理员权限有:', end='')
        for privilege in self.privileges:
            print(privilege + ',', end=' ')


admin = Admin('San', 'Zhang', '20')

# 打印用户信息并问好
admin.describe_user()
admin.greet_user()

# 调用管理员权限方法
admin.show_privileges()

运行结果:


9-8 权限

#!/usr/bin/env python
# -*- coding:utf-8 -*-


class User():
    """用户类"""
    def __init__(self, first_name, last_name, age):
        self.first_name = first_name
        self.last_name = last_name
        self.age = age
        self.login_attempts = 0

    def describe_user(self):
        """描述用户信息"""
        long_name = self.last_name + ' ' + self.first_name + ' ' + self.age
        print(long_name.title())

    def greet_user(self):
        """向用户问好"""
        print('Hello,' + ' ' + self.last_name + ' ' + self.first_name + '!')

    def increment_login_attempts(self):
        """计数登录次数"""
        self.login_attempts += 1

    def reset_login_attempts(self):
        """重置登录次数为0"""
        self.login_attempts = 0


class Privileges():
    """权限类"""
    def __init__(self):
        self.privileges = ['can add post', 'can delete post', 'can ban user']

    def show_privileges(self):
        print('管理员权限有:', end='')
        for privilege in self.privileges:
            print(privilege + ',', end=' ')


class Admin(User):
    """特殊用户Admin"""

    """初始化父类属性"""
    def __init__(self, first_name, last_name, age):
        super().__init__(first_name, last_name, age)  # 初始化父类属性
        self.privileges = Privileges()


admin = Admin('San', 'Zhang', '20')

# 打印用户信息并问好
admin.describe_user()
admin.greet_user()

# 调用管理员权限方法,注意这里还需要privileges句点法调用show_privileges()
admin.privileges.show_privileges()

运行结果:


9-10 导入Restaurant类

restaurant.py模块

#!/usr/bin/env python
# -*- coding:utf-8 -*-


class Restaurant():
    """模拟餐馆信息的简单尝试"""

    def __init__(self, restaurant_name, cuisine_type):
        """初始化描述餐馆的属性"""
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type
        self.number_served = 0

    def describe_restaurant(self):
        """返回餐馆信息"""
        long_name = self.restaurant_name + ' ' + self.cuisine_type
        return long_name.title()

    def open_restaurant(self):
        """打印餐厅营业消息"""
        print("The restaurant is opening!!")

    def set_number_served(self, numbers):
        """设置就餐人数"""
        self.number_served = numbers

    def increment_number_served(self, numbers):
        self.number_served += numbers

    def read_number_served(self):
        """打印一条就餐人数信息"""
        print("There are " + str(self.number_served) + ' people eating in this restaurant')


class IceCreamStand(Restaurant):
    def __init__(self, restaurant_name, cuisine_type):
        """初始化父类的属性"""
        super().__init__(restaurant_name, cuisine_type)  # 初始化父类的属性

        self.flavors = []

    def set_flavors(self, flavors):
        """设置冰淇淋餐厅口味"""
        self.flavors = flavors

    def read_flavors(self):
        """打印餐厅冰淇淋口味"""
        print('The taste of ice cream has:', end='')
        for flavor in self.flavors:
            print(flavor, end=' ')

主程序my_restaurant.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-

from restaurant import IceCreamStand


# 创建冰淇淋餐厅实例
my_icecreamstand = IceCreamStand('欢欢', '冰淇淋')

# 打印冰淇淋信息
print(my_icecreamstand.describe_restaurant())

# 打印冰淇淋口味
my_icecreamstand.set_flavors(['蓝莓', '巧克力', '芒果', '草莓'])
my_icecreamstand.read_flavors()

运行结果:


9-11 导入Admin类

admin.py模块

#!/usr/bin/env python
# -*- coding:utf-8 -*-


class User():
    """用户类"""
    def __init__(self, first_name, last_name, age):
        self.first_name = first_name
        self.last_name = last_name
        self.age = age
        self.login_attempts = 0

    def describe_user(self):
        """描述用户信息"""
        long_name = self.last_name + ' ' + self.first_name + ' ' + self.age
        print(long_name.title())

    def greet_user(self):
        """向用户问好"""
        print('Hello,' + ' ' + self.last_name + ' ' + self.first_name + '!')

    def increment_login_attempts(self):
        """计数登录次数"""
        self.login_attempts += 1

    def reset_login_attempts(self):
        """重置登录次数为0"""
        self.login_attempts = 0


class Privileges():
    """权限类"""
    def __init__(self):
        self.privileges = ['can add post', 'can delete post', 'can ban user']

    def show_privileges(self):
        print('管理员权限有:', end='')
        for privilege in self.privileges:
            print(privilege + ',', end=' ')


class Admin(User):
    """特殊用户Admin"""

    """初始化父类属性"""
    def __init__(self, first_name, last_name, age):
        super().__init__(first_name, last_name, age)  # 初始化父类属性
        self.privileges = Privileges()

主程序my_admin.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-

# 从admin模块导入Admin类
from admin import Admin

admin = Admin('Si', 'Li', '20')

# 打印用户信息并问好
admin.describe_user()
admin.greet_user()

# 调用管理员权限方法,注意这里还需要privileges句点法调用show_privileges()
admin.privileges.show_privileges()

运行结果:


9-12 多个模块

user.py模块

#!/usr/bin/env python
# -*- coding:utf-8 -*-


class User():
    """用户类"""
    def __init__(self, first_name, last_name, age):
        self.first_name = first_name
        self.last_name = last_name
        self.age = age
        self.login_attempts = 0

    def describe_user(self):
        """描述用户信息"""
        long_name = self.last_name + ' ' + self.first_name + ' ' + self.age
        print(long_name.title())

    def greet_user(self):
        """向用户问好"""
        print('Hello,' + ' ' + self.last_name + ' ' + self.first_name + '!')

    def increment_login_attempts(self):
        """计数登录次数"""
        self.login_attempts += 1

    def reset_login_attempts(self):
        """重置登录次数为0"""
        self.login_attempts = 0

admin.py模块

#!/usr/bin/env python
# -*- coding:utf-8 -*-

# 从user模块导入User类
from user import User


class Privileges():
    """权限类"""
    def __init__(self):
        self.privileges = ['can add post', 'can delete post', 'can ban user']

    def show_privileges(self):
        print('管理员权限有:', end='')
        for privilege in self.privileges:
            print(privilege + ',', end=' ')


class Admin(User):
    """特殊用户Admin"""

    """初始化父类属性"""
    def __init__(self, first_name, last_name, age):
        super().__init__(first_name, last_name, age)  # 初始化父类属性
        self.privileges = Privileges()

 主程序my_admin.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-


# 从admin模块导入Admin类
from admin import Admin

admin = Admin('Hua', 'Li', '15')

# 打印用户信息并问好
admin.describe_user()
admin.greet_user()

# 调用管理员权限方法,注意这里还需要privileges句点法调用show_privileges()
admin.privileges.show_privileges()

运行结果:

你可能感兴趣的:(Python编程)