实例化子类的正确方法? [英] Proper way to instantiate subclasses?
本文介绍了实例化子类的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我正在做一个小型的Pygame程序,并且试图以一种体面的外观来构造这些东西,这就是我所坚持的地方.
So I'm doing a small Pygame programm and I'm trying to structure the stuff in decent look, this is where I stucked.
class Screen(object):
def __init__(self, width, height):
self.width = width
self.height = height
self.SURFACE = 10 # I need this attribute
class GameMenager(Screen):
def __init__(self) # Do I need to put the parameters of the super class here?
super().__init__() # What to put in these parantheses?
我想获得 SURFACE
属性,以便可以在Game类中使用它,与标注属性相同.实例化类 Screen
和 GameMenager
的正确方法是什么?
I want to get the SURFACE
attribute so I can work with it in the Game class, same with the dimension attributes. What is the proper way to instantiate the classes Screen
and GameMenager
?
我对 super()
表示红色,但是实际上我没有找到关于在子类中没有参数时如何实例化子类的更多信息.
I red about super()
but didn't actualy find much information on how to instantiate subclasses when I don't have parameters in the subclass.
推荐答案
super
的用法如下:
super
is used like this:
class Screen(object):
def __init__(self, width, height):
self.width = width
self.height = height
self.SURFACE = 10
class GameMenager(Screen):
def __init__(self, width, height):
super(GameMenager, self).__init__(width, height)
现在,您可以访问 Screen.SURFACE
:
>>> x = GameMenager(1, 2) # I just picked 1 and 2 for width and height.
>>> x.SURFACE
10
>>>
这篇关于实例化子类的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文