9-1:
class Restaurant(): def __init__(self, restaurant_name, cuisine_type): self.restaurant_name = restaurant_name self.cuisine_type = cuisine_type def describe_restaurant(self): print(self.cuisine_type, self.restaurant_name) def open_restaurant(self): print('restaurant is open now!') restaurant = Restaurant('Wille', 'Lucy') print('Wille', 'Lucy') restaurant.describe_restaurant() restaurant.open_restaurant()
9-4:
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): print(self.cuisine_type, self.restaurant_name) def open_restaurant(self): print('restaurant is open now!') def set_number_served(self, number): self.number_served = number def increment_number_served(self, num): self.number_served += num restaurant = Restaurant('Wille', 'Lucy') print('在这家餐馆就过餐的人数'+str(restaurant.number_served)) restaurant.number_served = 100 print('在这家餐馆就过餐的人数'+str(restaurant.number_served)) restaurant.set_number_served(1000) print('在这家餐馆就过餐的人数'+str(restaurant.number_served)) restaurant.increment_number_served(50) print('在这家餐馆就过餐的人数'+str(restaurant.number_served))
9-6:
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): print(self.cuisine_type, self.restaurant_name) def open_restaurant(self): print('restaurant is open now!') def set_number_served(self, number): self.number_served = number def increment_number_served(self, num): self.number_served += num class IceCreamStand(Restaurant): def __init__(self, restaurant_name, cuisine_type, flavors): super().__init__(restaurant_name, cuisine_type) self.flavors = flavors def print_flavors(self): print('冰激凌口味:', end=' ') for item in self.flavors: print(item, end=' ') icecreamstand = IceCreamStand('Wahaha', 'yeah', ['Riso', 'Cioccolato', 'caffe', 'Pistacchio']) icecreamstand.print_flavors()
9-10:
from test import Restaurant re = Restaurant('hhh', 'sss') re.describe_restaurant()
9-13:
from collections import OrderedDict vo = OrderedDict() vo['del'] = '删除' vo['low()'] = '将所有字母小写' vo['upper()'] = '将所有字母大写' vo['append()'] = '添加一个元素' vo['print()'] = '输出函数' for ker, value in vo.items(): print(ker, value)
9-14:
from random import randint class Die(): def __init__(self, sides=6): self.sides = sides def roll_die(self): print(randint(1, self.sides)) die = Die() for i in range(0, 10): die.roll_die() die10 = Die(10) for i in range(0, 10): die10.roll_die() die20 = Die(20) for i in range(0, 10): die20.roll_die()