【发布时间】:2018-07-05 13:22:03
【问题描述】:
我尝试在这里做一个玩具类
class Toy:
def __init__(self,name,ID,price,age):
self.__ToyName = name
self.__ToyID = ID
self.__Price = price
self.__MinimumAge = int(age)
##some methods here
当我尝试制作子类电脑游戏时,我需要制作 7 个参数(其中 5 个来自玩具类)来实例化电脑游戏类,它显示“争论太多(7/5)”
class ComputerGame(Toy):
def __init__(self,name,ID,price,age,catogory,console):
Toy.__init__(self,name,ID,price,age)
self.__Catogory = catogory
self.__Console = console
遇到这种情况我该怎么办?
【问题讨论】:
-
self.toy = Toy(name,ID,price,age)或类似的就是你想要的。查看 dup 了解更多详情。 -
我不知道你做了什么,因为我发现你的代码没有问题,而且我的 python 也没有......
-
我会写
super().__init__(name, ID, price, age)但要小心,super不像其他语言那样工作。你的代码是正确的,寿。你应该做ComputerGame(name, ID, price, age, category, console) -
另外,除非您真的知道自己在做什么以及为什么,否则不要使用以双下划线开头的类属性名称。见 (stackoverflow.com/questions/1301346/…)
标签: python python-3.x class variables