在子类和super()函数中了解__init __()的麻烦
我在Python中理解这两个特定概念时遇到了一些麻烦。我觉得自己有要点,但不完全理解它们。
从我收集的内容来看,super()从父班上调用init方法,并处理您从父类传递的参数。
那么,为什么需要在子类中有一个INIT方法,其中包含与超类中相同的参数?这是因为两个类的性质仍然相似,因此子类需要超级类的基本特征?我知道大多数类都需要一个构造函数,只是想知道为什么您需要像超级类中的子类构造函数中重新定义相同的参数,并且在该构造函数的主体中具有super()函数。
代码的前任会是这样的:
#9-6 Ice Cream Stand
class Restaurant:
def __init__(self, restaurant_name, cuisine_type):
"""Set attributes of a restaurant"""
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
self.number_served = 0
def describe_restaurant(self):
"""Print a statement about the restaurant"""
print(f"Welcome to {self.restaurant_name.title()}! Enjoy our amazing {self.cuisine_type.title()} cuisine!")
def open_restaurant(self):
"""Print a message indicating the restaurant is open"""
print(f"{self.restaurant_name.title()} is open and ready to serve!")
def set_number_served(self, total_served):
self.number_served = total_served
def increment_number_served(self, additional_served):
self.number_served += additional_served
class IceCreamStand(Restaurant):
def __init__(self, restaurant_name, cuisine_type='ice cream'):#Same params as in superclass contructor
"""Initialize the attributes of the parent class
Then initialize the attributes specific to an ice cream stand."""
super().__init__(restaurant_name, cuisine_type)
self.flavors = []
def display_flavors(self):
"""Display the ice cream flavors"""
for flavor in self.flavors:
print(f"- " + f"{flavor}")
ic_stand1 = IceCreamStand('tasty ice cream')
ic_stand1.flavors = ['chocolate', 'vanilla', 'strawberry']
ic_stand1.display_flavors()
ic_stand1.describe_restaurant()
有人可以为我友善地漫步,以便我可以更好地了解这些概念吗?所有的帮助都得到赞赏。
I'm having a little trouble understanding these two particular concepts in Python. I feel like I get the gist of them, but don't understand them completely.
From what I have gathered, super() calls the init method from the parent class and handles the arguments you pass to it from the parent class.
Why then would there need to be an init method in the subclass that includes the same parameters as those in the superclass? Is it because both classes are still similar in nature and the subclass would therefore require some of the basic features of the superclass? I know that most classes require a constructor, just wondering why you would need to re-define the same parameters in a subclass constructor like those in the superclass, and have a super() function within the body of that constructor.
An ex of code, would be something like this:
#9-6 Ice Cream Stand
class Restaurant:
def __init__(self, restaurant_name, cuisine_type):
"""Set attributes of a restaurant"""
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
self.number_served = 0
def describe_restaurant(self):
"""Print a statement about the restaurant"""
print(f"Welcome to {self.restaurant_name.title()}! Enjoy our amazing {self.cuisine_type.title()} cuisine!")
def open_restaurant(self):
"""Print a message indicating the restaurant is open"""
print(f"{self.restaurant_name.title()} is open and ready to serve!")
def set_number_served(self, total_served):
self.number_served = total_served
def increment_number_served(self, additional_served):
self.number_served += additional_served
class IceCreamStand(Restaurant):
def __init__(self, restaurant_name, cuisine_type='ice cream'):#Same params as in superclass contructor
"""Initialize the attributes of the parent class
Then initialize the attributes specific to an ice cream stand."""
super().__init__(restaurant_name, cuisine_type)
self.flavors = []
def display_flavors(self):
"""Display the ice cream flavors"""
for flavor in self.flavors:
print(f"- " + f"{flavor}")
ic_stand1 = IceCreamStand('tasty ice cream')
ic_stand1.flavors = ['chocolate', 'vanilla', 'strawberry']
ic_stand1.display_flavors()
ic_stand1.describe_restaurant()
Can anyone kindly walk this through for me, so I can get a better picture and grasp of these concepts? All help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
构建
iCecreamstand
的过程涉及构建resturant
的过程,以及其他一些东西(例如初始化flavors
)。要构建resturant
,您需要restaurant_name
和cuisine_type
。但是
__ INIT __
iCecreamstand
不知道您想要什么名字。因此,就像resturant
要求您提供restaurant_name
的方式一样,icecreamstand也是如此。
这里的一个奇数是,
__ INIT __
iCecreamstand
具有cuisine_type
默认为'冰淇淋'
。这是没有意义的,因为它可以允许呼叫者说:相反,将其定义这样的定义是有意义的:
The process of constructing an
IceCreamStand
involves the process of constructing aResturant
, plus some other stuff (e.g. initialize theflavours
). To construct aResturant
, you need therestaurant_name
and thecuisine_type
.But the
__init__
ofIceCreamStand
doesn't know what name you want. So just like how aResturant
asks you to provide therestaurant_name
, so does theIceCreamStand
.The one odd-ball here is that the
__init__
ofIceCreamStand
has acuisine_type
defaulted to'ice cream'
. This doesn't make sense, because it would allow a caller to say:Instead, it would make sense to define it like so: