当前位置:   article > 正文

作业九——类

作业九——类

     该章主要讲述了如何使用类,构造类,还有关于类的继承的问题。

  

9-1 餐馆

    题目描述:      

           创建一个名为Restaurant 的类,其方法__init__() 设置两个属性:restaurant_name 和cuisine_type 。创建一个名
为describe_restaurant() 的方法和一个名为open_restaurant() 的方法,其中前者打印前述两项信息,而后者打印一条消息,指出餐馆正在营业。

    INPUT:

            None

    OUTPUT:    

            None

    代码展示:

  1. class Restaurant():
  2. def __init__(self, restaurant_name, cuisine_type):
  3. self.restaurant_name = restaurant_name
  4. self.cuisine_type = cuisine_type
  5. def describe_restaurant(self):
  6. print("The name of this restaurant is " + self.restaurant_name + ".")
  7. print("The cuisine type of this restaurant is " + self.cuisine_type + ".")
  8. def open_restaurant(self):
  9. print(self.restaurant_name.title() + " is open now!")
 

9-4 就餐人数

    题目描述:

          在为完成练习9-1而编写的程序中,添加一个名为number_served 的属性,并将其默认值设置为0。根据这个类创建一个名为restaurant 的实例;打印有多少人在这家餐馆就餐过,然后修改这个值并再次打印它。
           添加一个名为set_number_served() 的方法,它让你能够设置就餐人数。调用这个方法并向它传递一个值,然后再次打印这个值。
           添加一个名为increment_number_served() 的方法,它让你能够将就餐人数递增。调用这个方法并向它传递一个这样的值:你认为这家餐馆每天可能接待的就餐人数。

    INPUT:

            None

    OUTPUT:    

  1. The name of this restaurant is Sunny.
  2. The cuisine type of this restaurant is BBQ.
  3. The number of this restaurant served is 0.
  4. After setting:
  5. The name of this restaurant is Sunny.
  6. The cuisine type of this restaurant is BBQ.
  7. The number of this restaurant served is 59.
  8. After increment:
  9. The name of this restaurant is Sunny.
  10. The cuisine type of this restaurant is BBQ.
  11. The number of this restaurant served is 60.

    代码展示:

  1. class Restaurant():
  2. def __init__(self, restaurant_name, cuisine_type):
  3. self.restaurant_name = restaurant_name
  4. self.cuisine_type = cuisine_type
  5. self.number_served = 0
  6. def describe_restaurant(self):
  7. print("The name of this restaurant is " + self.restaurant_name + ".")
  8. print("The cuisine type of this restaurant is " + self.cuisine_type + ".")
  9. print("The number of this restaurant served is " + str(self.number_served) + ".")
  10. def open_restaurant(self):
  11. print(self.restaurant_name.title() + " is open now!")
  12. def set_number_served(self, num):
  13. self.number_served = num
  14. def increment_number_served(self):
  15. self.number_served = self.number_served + 1
  16. SunnyRestaurant = Restaurant("Sunny", "BBQ")
  17. SunnyRestaurant.describe_restaurant()
  18. SunnyRestaurant.set_number_served(59)
  19. print("\nAfter setting:")
  20. SunnyRestaurant.describe_restaurant()
  21. SunnyRestaurant.increment_number_served()
  22. print("\nAfter increment:")
  23. SunnyRestaurant.describe_restaurant()


9-13 使用OrderedDict

    题目描述:        

            在练习6-4中,你使用了一个标准字典来表示词汇表。请使用OrderedDict 类来重写这个程序,并确认输出的顺序与你在字典中添加键
—值对的顺序一致。

    INPUT:

            None

    OUTPUT:    

  1. Cuisine : a style or method of cooking, especially as characteristic of a particular country, region, or establishment.
  2. Strict : demanding that rules concerning behavior are obeyed and observed.
  3. Confine : keep or restrict someone or something within certain limits of
  4. Quick : moving fast or doing something in a short time.
  5. Language : The system of communication used by a particular community or country.

    代码展示:

  1. from collections import OrderedDict
  2. dictionary = OrderedDict()
  3. dictionary['cuisine'] = 'a style or method of cooking, especially as characteristic ' \
  4. 'of a particular country, region, or establishment.'
  5. dictionary['strict'] = 'demanding that rules concerning behavior are obeyed and observed.'
  6. dictionary['confine'] = 'keep or restrict someone or something within certain limits ' \
  7. 'of'
  8. dictionary['quick'] = 'moving fast or doing something in a short time.'
  9. dictionary['language'] = 'The system of communication used by a particular community or ' \
  10. 'country.'
  11. for key, item in dictionary.items():
  12. print(key.title() + " : " + item)

9-14 骰子 

    题目描述:

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

    INPUT:

            None

    OUTPUT:    

  1. This time is 4 !
  2. This time is 4 !
  3. This time is 4 !
  4. This time is 6 !
  5. This time is 6 !
  6. This time is 5 !
  7. This time is 5 !
  8. This time is 5 !
  9. This time is 1 !
  10. This time is 4 !
  11. This time is 5 !
  12. This time is 1 !
  13. This time is 1 !
  14. This time is 2 !
  15. This time is 5 !
  16. This time is 1 !
  17. This time is 3 !
  18. This time is 3 !
  19. This time is 2 !
  20. This time is 2 !

    代码展示:

  1. from random import randint
  2. class Die():
  3. def __init__(self):
  4. self.sides = 6
  5. def roll_die(self):
  6. print("This time is " + str(randint(1, 6)) + " !")
  7. tmp = Die()
  8. for i in range(0, 20):
  9. tmp.roll_die()
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/992909
推荐阅读
相关标签
  

闽ICP备14008679号