On 19Jun2017 12:32, Evuraan <evur...@gmail.com> wrote:
Greetings!
Hi!
#!/usr/bin/python3
class Employee:
"""Class with FirstName, LastName, Salary"""
def __init__(self, FirstName,LastName, Salary):
self.FirstName = FirstName
self.LastName = LastName
self.Salary = Salary
def __str__(self):
return '("{}" "{}" "{}")'.format(self.FirstName,
self.LastName, self.Salary)
class Developer(Employee):
"""Define a subclass, augment with ProgLang"""
def __init__(self, FirstName,LastName, Salary, ProgLang):
Employee.__init__(self, FirstName,LastName, Salary)
self.ProgLang = ProgLang
def dev_repr(self):
return '("{}" "{}" "{}" "{}")'.format(self.FirstName,
self.LastName, self.Salary, self.ProgLang)
a = Employee("Abigail", "Buchard", 83000)
print(a)
dev_1 = Developer("Samson", "Sue", 63000, "Cobol",)
print(dev_1)
print(dev_1.dev_repr())
running that yields,
("Abigail" "Buchard" "83000")
("Samson" "Sue" "63000")
("Samson" "Sue" "63000" "Cobol")
My doubt is, how can we set the __str__ method work on the Employee
subclass so that it would show ProgLang too, like the
print(dev_1.dev_repr())?
Assuming that when you say "the Employee subclass" above you mean "Developer",
just like any other method you would override in a subclass. When you define
Developer, define a __str__ method:
class Developer(Employee):
...
def __str__(self):
return ..... same expression as dev_repr ...
Broadly speaking, a subclass is "just like" the parent, except as you specify.
So specify __str__, since you want it to be different.
Cheers,
Cameron Simpson <c...@zip.com.au>
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor