How to add the contents of a parent class method to a subclass method

Ok, I'm new to OOP, and my problem is that I have this parent class and it has information about the method in it. I want to reuse the two print statements in this method for my subclass inside the method of the same name and add additional information to it. but i cant make it work

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()

I thought I was just rewriting the lines of code that I want to reuse, but although that might take the OOP point.

I am looking for such a conclusion ...

Name: John Doe
Salary: 1000
Programming language: Python

I am using Python 3

Thanks in advance!

+4
source share
1 answer

. "object". super

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()
+1

Source: https://habr.com/ru/post/1659304/


All Articles