class User: def __init__(self,first_name,last_name): """初始化姓和名""" self.f_name = first_name self.l_name = last_name def describe_user(self): """返回整洁的描述性姓名""" full_name = f"{self.f_name}{self.l_name}" return full_name def greest_user(self): """问候语""" print(f"恭喜{self.describe_user()}完成了本次练习") class Admin: """初始化管理类的属性""" def __init__(self,privileges = ["添加帖子","删除帖子","禁止帖子"]): self.privileges = privileges def show_privieges(self): """描述管理特权的信息""" print(f"管理员的特权:{self.privileges}") class Privileges(User): """初始化特权的属性""" def __init__(self,first_name,last_name): super().__init__(first_name,last_name) self.admin = Admin() my_name = Privileges('叶','辰') print(my_name.describe_user()) my_name.greest_user() my_name.admin.show_privieges() your_name = User('林','凡') print(your_name.describe_user()) your_name.greest_user() who_name = User('萧','峰') print(who_name.describe_user()) who_name.greest_user()
class Restaurant: """创建一个名为餐馆的类""" def __init__(self,name,type): """初始化餐馆的名字和菜式类型""" self.restaurant_name = name self.cuisine_type = type def get_descriptive(self): """描述信息""" descriptive = f"这个餐厅的名字叫{self.restaurant_name},其擅长{self.cuisine_type}" return descriptive def open_restaurant(self): """餐馆正在营业""" print(f"{self.restaurant_name}正在营业") class IceCreamStand(Restaurant): """ 初始化父类的属性 再初始化冰淇淋的属性 """ def __init__(self,name,type): super().__init__(name,type) # super(). 直接调用父类的方法 self.flavors = ['香草味','香芋味','菠萝味','抹茶','巧克力','奶油泡芙','奶油泡芙','圣代冰淇淋'] def descriptive_icecream(self): """描述冰淇淋的消息""" print(f"冰淇淋的口味如下:{self.flavors}") my_restaurant = Restaurant('杰西庄','五湖四海各式各样的菜式') print(my_restaurant.get_descriptive()) my_restaurant.open_restaurant() my_ice = IceCreamStand('甜心','各种不同口味的冰淇淋') print(my_ice.get_descriptive()) my_ice.open_restaurant() my_ice.descriptive_icecream()
继承可以把父类的所有功能都直接拿过来,不用重零做起,子类只需要新增自己特有的方法,也可以把父类不适合的方法覆盖重
标签:__,name,python,self,----,12.15,print,my,def From: https://www.cnblogs.com/jc13052/p/17901021.html