继承
一个类继承另一个类时,将自动获得另一个类的所有属性和方法,原有的类称为父类,而新的类称为子类
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=f"{self.year} {self.make} {self.model}"
return long_name.title()
def read_odometer(self):
print(f"This car has {self.odometer_reading}miles on it")
def update_odometer(self,mileage):
if mileage>self.odometer_reading:
self.odometer_reading=mileage
else:
print("You cannot roll back an odometer!")
def increment_odometer(self,miles):
self.odometer_reading+=miles
class ElectricCar(Car):
def __init__(self,make,model,year):
super().__init__(make,model,year)
self.battery_size=75
def describe_battery(self):
print(f"This car has {self.battery_size}kwh battery")
my_tesla=ElectricCar('tesla','models',2019)
print(my_tesla.get_descriptive_name())
my_tesla.describe_battery()
#练习9-6
class Restaurant():
"""docstring for 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(f"名字是:{self.restaurant_name},烹饪风味是{self.cuisine_type},有{self.number_served}人就餐")
def open_restaurant(self):
print(f"{self.restaurant_name}餐厅正在营业")
def set_number_served(self,number):
self.number_served=number
def increment_number_served(self,inumber):
self.number_served+=inumber
class IceCreamStand(Restaurant):
def __init__(self,restaurant_name,cuisine_type):
super().__init__(restaurant_name,cuisine_type)
self.flavors=['蓝莓味','草莓味','橘子味']
def describe_iceCream(self):
print(f"这里的冰淇凌有:{self.flavors}")
the_restaurant=IceCreamStand('十里八湘','川菜馆')
the_restaurant.set_number_served(16)
the_restaurant.describe_restaurant()
the_restaurant.increment_number_served(10)
the_restaurant.describe_restaurant()
the_restaurant.describe_iceCream()
"""
名字是:十里八湘,烹饪风味是川菜馆,有16人就餐
名字是:十里八湘,烹饪风味是川菜馆,有26人就餐
这里的冰淇凌有:['蓝莓味', '草莓味', '橘子味']
"""
#练习9-7
class User:
def __init__(self,first_name,last_name,gender,login_attempts):
self.first_name=first_name
self.last_name=last_name
self.gender=gender
self.login_attempts=login_attempts
def describe_user(self):
print(f"姓:{self.first_name},名:{self.last_name},性别:{self.gender}")
def greet_user(self):
print(f"欢迎{self.first_name}{self.last_name}{self.gender}士!!!")
def increment_login_attempts(self):
self.login_attempts+=1
print(f"现有人数{self.login_attempts}")
def reset_login_attempts(self):
self.login_attempts=0
print(f"现有人数{self.login_attempts}")
class Admin(User):
def __init__(self,first_name,last_name,gender,login_attempts):
super().__init__(first_name,last_name,gender,login_attempts)
self.privileges=['can add post','can delete post','can be user']
def show_privileges(self):
print(f"Admin用户的权限有:{self.privileges}")
user1=Admin('王','飞','男',1)
user1.describe_user()
user1.increment_login_attempts()
user1.increment_login_attempts()
user1.reset_login_attempts()
user1.greet_user()
user1.show_privileges()
"""
姓:王,名:飞,性别:男
现有人数2
现有人数3
现有人数0
欢迎王飞男士!!!
Admin用户的权限有:['can add post', 'can delete post', 'can be user']
"""
python标准库
from random import randint
print(randint(1,6))
列表中随便选一个
from random import choice
players=['李小龙','甄子丹','李连杰','成龙']
print(choice(players))
#练习9-13
from random import randint
class Die(object):
def __init__(self,sides=6):
self.sides=sides
def roll_die(self):
return randint(1,self.sides)
d6=Die(6)
results=[]
for roll_num in range(10):
result=d6.roll_die()
results.append(result)
print("6面10次的筛子结果:")
print(results)
d10=Die(10)
results=[]
for roll_num in range(10):
result=d10.roll_die()
results.append(result)
print("10面10次的筛子结果:")
print(results)
d20=Die(20)
results=[]
for roll_num in range(10):
result=d20.roll_die()
results.append(result)
print("20面10次的筛子结果:")
print(results)
"""
6面10次的筛子结果:
[4, 2, 6, 3, 2, 3, 1, 2, 6, 1]
10面10次的筛子结果:
[2, 4, 4, 9, 10, 6, 8, 5, 2, 6]
20面10次的筛子结果:
[16, 13, 3, 14, 17, 7, 1, 19, 3, 16]
"""
|