动手试一试
9.1餐馆
class Restaurant():
def __init__(self,name,type):
self.name=name;
self.type=type;
def describe_restaurant(self):
print("The restaurant name is "+self.name+" !");
print("The restaurant type is "+self.type+" !");
def open_restaurant(self):
print("The restaurant is running!");
a=Restaurant("guogang","S");
Restaurant.describe_restaurant(a);
Restaurant.open_restaurant(a)
9.3用户
class use():
def __init__(self,firstname,lastname):
self.firstname=firstname;
self.lastname=lastname;
def describe_use(self):
print("The use name is "+self.firstname+","+self.lastname+" !");
def greet_use(self):
print("hello!"+self.firstname+","+self.lastname);
a=use("guo","gang");
a.describe_use();
a.greet_use();
9.4就餐人数
class Restaurant():
def __init__(self,name,type):
self.name=name;
self.type=type;
self.account=0;
def describe_restaurant(self):
print("The restaurant name is "+self.name+" !");
print("The restaurant type is "+self.type+" !");
def open_restaurant(self):
print("The restaurant is running!");
def set_number(self,account):
self.account=account;
def inc_number(self,and1):
if and1 > 0:
self.account+=and1;
a=Restaurant("guogang","001");
a.set_number(10);
a.inc_number(5);
print(a.account);
9.6冰淇淋小店
class Restaurant():
def __init__(self,name,type):
self.name=name;
self.type=type;
self.account=0;
def describe_restaurant(self):
print("The restaurant name is "+self.name+" !");
print("The restaurant type is "+self.type+" !");
def open_restaurant(self):
print("The restaurant is running!");
def set_number(self,account):
self.account=account;
def inc_number(self,and1):
if and1 > 0:
self.account+=and1;
class Iceroom(Restaurant):
def __init__(self,name,type):
super().__init__(name,type)
self.fav=[];
def faver(self,ice):
for cell in ice:
self.fav.append(cell);
a=Iceroom("guogang","001");
b=["A","B","C"];
a.faver(b);
print(a.fav);
9.10导入Restaurant类
-Restaurant与Iceroom类的模块
class Restaurant():
def __init__(self,name,type):
self.name=name;
self.type=type;
self.account=0;
def describe_restaurant(self):
print("The restaurant name is "+self.name+" !");
print("The restaurant type is "+self.type+" !");
def open_restaurant(self):
print("The restaurant is running!");
def set_number(self,account):
self.account=account;
def inc_number(self,and1):
if and1 > 0:
self.account+=and1;
class Iceroom(Restaurant):
def __init__(self,name,type):
super().__init__(name,type)
self.fav=[];
def faver(self,ice):
for cell in ice:
self.fav.append(cell);
a=Iceroom("guogang","001");
b=["A","B","C"];
a.faver(b);
print(a.fav);
-new-restanrant模块
from Restaurant import Iceroom
d=Iceroom("guogang","001");
9.14骰子
from random import randint
class Die():
def __init__(self,account):
self.account=account;
def roll1(self,account):
x=randint(1,account);
return x;
a=Die(6);
count=10;
while count > 0:
print(a.roll1(6));
count-=1;