9-1 餐馆:
代码如下:
class Restaurant():
def __init__(self, name, type):
self.name=name
self.type=type
def open(self):
print("The restaurant now is opening.")
def describe(self):
print("The name of restaurant is "+self.name+", and it's tpye is "+self.type+".")
AA=Restaurant('AAA',"Chinese food")
AA.describe()
AA.open()
运行结果:
9-4 就餐人数:
代码如下:
class Restaurant():
def __init__(self, name, type):
self.name=name
self.type=type
self.number_served=0
def open(self):
print("The restaurant now is opening.")
def describe(self):
print("The name of restaurant is "+self.name+", and it's tpye is "+self.type+".")
def set_number_served(self,num):
self.number_served=num
def increment_number_served(self,num2):
self.number_served=self.number_served+num2
AA=Restaurant('AAA',"Chinese food")
AA.describe()
print("And it served "+str(AA.number_served)+". ")
AA.set_number_served(10)
print("And it served "+str(AA.number_served)+" after a while. ")
AA.increment_number_served(24)
print("And it served "+str(AA.number_served)+" finally. ")
结果如下:
9-6 冰淇淋小店:
代码如下:
class Restaurant():
def __init__(self, name, type):
self.name=name
self.type=type
self.number_served=0
def open(self):
print("The restaurant now is opening.")
def describe(self):
print("The name of restaurant is "+self.name+", and it's tpye is "+self.type+".")
def set_number_served(self,num):
self.number_served=num
def increment_number_served(self,num2):
self.number_served=self.number_served+num2
class IceCreamStand(Restaurant):
def __init__(self, name,type,*flavors):
super().__init__(name ,type)
self.fla=[]
for i in flavors:
self.fla.append(i)
def show_fla(self):
print("This shop have many flavors of icecream, such as")
for i in self.fla:
print("--"+i)
AA=IceCreamStand("YXX","ice-cream",'strawberry','mint','apple')
AA.show_fla()
结果如下:
9-10 导入Restaurant 类:
代码如下:
class1:
class Restaurant():
def __init__(self, name, type):
self.name=name
self.type=type
self.number_served=0
def open(self):
print("The restaurant now is opening.")
def describe(self):
print("The name of restaurant is "+self.name+", and it's tpye is "+self.type+".")
def set_number_served(self,num):
self.number_served=num
def increment_number_served(self,num2):
self.number_served=self.number_served+num2
class IceCreamStand(Restaurant):
def __init__(self, name,type,*flavors):
super().__init__(name ,type)
self.fla=[]
for i in flavors:
self.fla.append(i)
def show_fla(self):
print("This shop have many flavors of icecream, such as")
for i in self.fla:
print("--"+i)
main:
from class1 import Restaurant
AA=Restaurant("YXX","ice-cream")
AA.describe()
AA.open()
结果如下:
9-12 多个模板:
代码如下:
class1:
class Restaurant():
def __init__(self, name, type):
self.name=name
self.type=type
self.number_served=0
def open(self):
print("The restaurant now is opening.")
def describe(self):
print("The name of restaurant is "+self.name+", and it's tpye is "+self.type+".")
def set_number_served(self,num):
self.number_served=num
def increment_number_served(self,num2):
self.number_served=self.number_served+num2
class2:
from class1 import Restaurant
class IceCreamStand(Restaurant):
def __init__(self, name,type,*flavors):
super().__init__(name ,type)
self.fla=[]
for i in flavors:
self.fla.append(i)
def show_fla(self):
print("This shop have many flavors of icecream, such as")
for i in self.fla:
print("--"+i)
main:
from class2 import IceCreamStand
AA=IceCreamStand("YXX","ice-cream","A","B")
AA.describe()
AA.open()
AA.show_fla()
结果如下:
9-14 骰子:
代码如下:
from random import randint
class Die():
def __init__(self,sides=6):
self.sides=sides
def roll_die(self):
x=randint(1,self.sides)
print(x,end=" ")
A=Die()
for i in range(1,11):
A.roll_die()
print(" ")
B=Die(10)
for i in range(1,11):
B.roll_die()
print(" ")
C=Die(20)
for i in range(1,11):
C.roll_die()
print(" ")
运行结果: