how to add contents of a parent class method into a subclass method
本问题已经有最佳答案,请猛点这里访问。
好吧,我刚接触OOP,我的问题是我有这个父类,它里面有一个方法信息。我想在一个同名的方法中重用该方法中的两个print语句作为它的子类,并向它添加更多信息。但我不能让它工作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | class company: def __init__(self, fName, lName, salary): self.fName = fName self.lName = lName self.salary = salary def info(self): print("Name:", self.fName, self.lName) print("Salary:", self.salary) class programmers(company): def __init__(self, fName, lName, salary, progLang): super().__init__(fName, lName, salary) self.progLang = progLang def info(self): print("Programming language:", self.progLang) programmer1 = programmers("John","Doe", 1000,"Python") programmer1.info() |
我想重写我想要重用的代码行,但我认为这会让OOP失去意义。
我正在寻找这种输出…
1 2 3 | Name: John Doe Salary: 1000 Programming language: Python |
我用的是python 3
事先谢谢!
试试下面的代码。您的父类应该从EDOCX1继承(0)。Super只适用于新风格的课程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | class company(object): def __init__(self, fName, lName, salary): self.fName = fName self.lName = lName self.salary = salary def info(self): print("Name:", self.fName, self.lName) print("Salary:", self.salary) class programmers(company): def __init__(self, fName, lName, salary, progLang): super(programmers, self).__init__(fName, lName, salary) self.progLang = progLang def info(self): super(programmers, self).info() print("Programming language:", self.progLang) programmer1 = programmers("John","Doe", 1000,"Python") programmer1.info() |