9-1 餐馆:
class Restaurant():
"""docstring for Restaurant"""
def __init__(self, restaurant_name, cuisine_type):
self.restaurant_name = restaurant_name
self.cuisine_type=cuisine_type
def describe_restaurant(self):
print(self.restaurant_name,self.cuisine_type)
def open_restaurant(self):
print("The restaurant is opening")
restaurant=Restaurant('bobo_restaurant','delicious')
print(restaurant.restaurant_name,restaurant.cuisine_type)
restaurant.describe_restaurant()
restaurant.open_restaurant()
运行结果:
9-2 三家餐馆:
restaurant1=Restaurant("lmd_restaurant",'good')
restaurant1.describe_restaurant()
restaurant2=Restaurant("hyk_restaurant",'very good')
restaurant2.describe_restaurant()
restaurant3=Restaurant("kq_restaurant",'so good')
restaurant3.describe_restaurant()
运行结果:
9-3 用户:
class User():
"""docstring for 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):
print("This is "+self.first_name.title()+' '+self.last_name.lower()+',ages '+str(self.age))
def greet_user(self):
print('Hello '+self.first_name.title()+' '+self.last_name.lower())
-
def deal(usr):
usr.describe_user()
usr.greet_user()
usr1=User('Ke','sibo',21)
deal(usr1)
usr2=User("Lao",'madong',20)
deal(usr2)
usr3=User("kuang",'qian',20)
deal(usr3)
运行结果:
9-4 就餐人数:
class Restaurant():
"""docstring for Restaurant"""
def __init__(self, restaurant_name, cuisine_type,number_served=0):
self.restaurant_name = restaurant_name
self.cuisine_type=cuisine_type
self.number_served=number_served
def describe_restaurant(self):
print(self.restaurant_name,self.cuisine_type,self.number_served)
def open_restaurant(self):
print("The restaurant is opening")
def set_number_served(self,number_served):
self.number_served=number_served
def increment_number_served(self,num):
self.number_served+=num
def show(self):
print(str(self.number_served)+' people has eat sth here')
restaurant=Restaurant('bobo_restaurant','delicious')
restaurant.show()
restaurant.number_served=3
restaurant.show()
restaurant.set_number_served(5)
restaurant.show()
restaurant.increment_number_served(3)
restaurant.show()
运行结果:
9-5 尝试登录次数:
class User():
"""docstring for User"""
def __init__(self, first_name,last_name,age,login_attempts):
self.first_name = first_name
self.last_name = last_name
self.age = age
self.login_attempts=login_attempts
def describe_user(self):
print("This is "+self.first_name.title()+' '+self.last_name.lower()+',ages '+str(self.age))
def greet_user(self):
print('Hello '+self.first_name.title()+' '+self.last_name.lower())
def increment_login_attempts(self):
self.login_attempts+=1
def reset_login_attempts(self):
self.login_attempts=0
def show(self):
print(self.login_attempts)
usr1=User('Ke','sibo',21,3)
usr1.show()
for i in range(10):
usr1.increment_login_attempts()
usr1.show()
usr1.reset_login_attempts()
usr1.show()
运行结果:
9-6 冰淇淋小店:
class IceCreamStand(Restaurant):
def __init__(self, restaurant_name, cuisine_type,number_served=0,flavors=[]):
super().__init__(restaurant_name, cuisine_type,number_served)
self.flavors=flavors[:]
def show(self):
for x in self.flavors:
print(x,end=' ')
flavors=['apples','banana','orange']
ic1=IceCreamStand("ke_restaurant",'good',100,flavors)
ic1.show()
运行结果:
9-7 管理员:
class Admin(User):
"""docstring for Admin"""
def __init__(self, first_name,last_name,age,login_attempts,privileges):
super().__init__(first_name,last_name,age,login_attempts)
self.privileges = privileges[:]
def show_privileges(self):
for x in self.privileges:
print(x)
arr=['can add post','can delete post','can ban user']
ad1=Admin('ke'.title(),'sibo'.lower(),22,0,arr)
ad1.show_privileges()
运行结果:
9-8 权限:
class Privileges(object):
"""docstring for Privileges"""
def __init__(self, privileges):
self.privileges = privileges
def show_privileges(self):
for x in self.privileges:
print(x)
class Admin(User):
"""docstring for Admin"""
def __init__(self, first_name,last_name,age,login_attempts,arr):
super().__init__(first_name,last_name,age,login_attempts)
self.privileges = Privileges(arr)
arr=['can add post','can delete post','can ban user']
ad1=Admin('ke'.title(),'sibo'.lower(),22,0,arr)
ad1.privileges.show_privileges()
运行结果:
9-9 电瓶升级:
class Car():
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0
def get_descriptive_name(self):
long_name = str(self.year) + ' ' + self.make + ' ' + self.model
return long_name.title()
def read_odometer(self):
print("This car has " + str(self.odometer_reading) + " miles on it.")
def update_odometer(self, mileage):
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print("You can't roll back an odometer!")
def increment_odometer(self, miles):
self.odometer_reading += miles
class Battery():
"""一次模拟电动汽车电瓶的简单尝试"""
def __init__(self, battery_size=70):
"""初始化电瓶的属性"""
self.battery_size = battery_size
def describe_battery(self):
"""打印一条描述电瓶容量的消息"""
print("This car has a " + str(self.battery_size) + "-kWh battery.")
def upgrade_battery(self):
self.battery_size=85 if self.battery_size!=85 else self.battery_size
def get_range(self):
"""打印一条消息,指出电瓶的续航里程"""
if self.battery_size == 70:
range = 240
elif self.battery_size == 85:
range = 270
message = "This car can go approximately " + str(range)
message += " miles on a full charge."
print(message)
class ElectricCar(Car):
"""电动汽车的独特之处"""
def __init__(self, make, model, year):
"""初始化父类的属性"""
super().__init__(make, model, year)
self.battery=Battery()
my_tesla = ElectricCar('tesla', 'models', 2016)
my_tesla.battery.get_range()
my_tesla.battery.upgrade_battery()
my_tesla.battery.get_range()
运行结果:
9-10 引入restaurant类:
import Restaurant
res=Restaurant.Restaurant('ke_restaurant', 'good',10)
res.describe_restaurant()
运行结果:
9-11 导入admin类:
import admin
arr=['can add post','can delete post','can ban user']
ad=admin.Admin('ke'.title(),'sibo'.lower(),22,0,arr)
ad.privileges.show_privileges()
运行结果:
9-12 多个模块:
import user
import admin
arr=['can add post','can delete post','can ban user']
ad=admin.Admin('ke'.title(),'sibo'.lower(),22,0,arr)
ad.privileges.show_privileges()
运行结果:
同上
9-13 使用OrderedDict:
from collections import OrderedDict
#不用orderdict的话,输出不一定按照添加的顺序(虽然试了好多次都是按原顺序输出的)
#但添加了这个就一定按原顺序输出
arr=OrderedDict()
arr['c']='C'
arr[(2,3)]='A'
arr[3]='B'
for key,value in arr.items():
print(key,value)
运行结果:
9-14 骰子:
from random import randint
class Die(object):
"""docstring for Die"""
def __init__(self, sides=6):
self.sides = sides
def roll_die(self):
print(randint(1,self.sides),end=' ')
def roll_10(self):
for i in range(10):
self.roll_die()
print()
touzi1=Die()
touzi1.roll_10()
touzi2=Die(10)
touzi2.roll_10()
touzi3=Die(20)
touzi3.roll_10()
执行结果:
9-15 略