Override


#1



13.Override

My code works, but not as I want

Is it possible to make my code looks without double ceo or emp???


I use python 3.5


class Employee(object):
    def __init__(self, name, surname):
        self.name = name
        self.surname=surname
    def greet(self, other, other1):
        print ("Hello, %s %s" %(other.name, other.surname))

class Ceo(Employee):
    def greet(self,other, other1 ):
        print ("Get back to work %s, %s" %(other.name, other1.surname)) 



ceo=Ceo("Katya", "Sobaka")
emp=Employee("Yan", "Makouski")

emp.greet(ceo,ceo), ceo.greet(emp,emp)

That would be printed in the shell:

Hello, Katya Sobaka
Get back to work Yan, Makouski


#2

Yes, you can. Each object already has the name and surname associated with it so all you have to do is pass the single object and then in the print statement pull the name and surname. Here is how your code would look to do that.

class Employee(object):
    def __init__(self, name, surname):
        self.name = name
        self.surname=surname
    def greet(self, other):
        print ("Hello, %s %s" %(other.name, other.surname))

class Ceo(Employee):
    def greet(self,other):
        print ("Get back to work %s, %s" %(other.name, other.surname)) 



ceo=Ceo("Katya", "Sobaka")
emp=Employee("Yan", "Makouski")

emp.greet(ceo), ceo.greet(emp)

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.