继承的父类是上一篇python类和实例里的类
1、冰淇淋小店:冰淇淋小店是一种特殊的餐馆。编写一个名为IceCreamStand的类,让它继承Restaurant类。这两个版本的Restaurant类都可以,挑选你更喜欢的那个即可。添加一个名为flavors的属性,用于存储一个由各种口味的冰淇淋组成的列表。编写一个显示这些冰淇淋的方法。创建一个IceCreamStand实例,并调用这个方法。
from lianxi00 importRestaurant,User classIceCreamStand(Restaurant): def __init__(self,restaurant_name,cuisine_type): super().__init__(restaurant_name,cuisine_type) self.flavors=['apple_wei','banana_wei','xigua_wei'] defshowAllWei(self): print("冰琪琳的味道有:"+str(self.flavors)) mycanting = IceCreamStand("冰淇淋","冰棍等") print(mycanting.describe_restaurant()) print(mycanting.showAllWei())
from lianxi00 importRestaurant,User classAdmin(User): def __init__(self,first_name,last_name,login_attempts): super().__init__(first_name,last_name,login_attempts) self.privileges=["can add post" , "can delete post" ,"can ban user"] defshow_privileges(self,first_name,last_name,login_attempts): user = first_name + last_name +str(login_attempts) if user=="admin10": for i inself.privileges: print("admin privileges is "+i) else: print("error !") myadmin=Admin("ad","min",10) myadmin.show_privileges("ad","min",10)