Python Forum
Help needed with polymorphism example
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help needed with polymorphism example
#2
There are two related problems in your classes. First, every class method must have at least one argument because the class instance is passed in as the first argument to a method. By convention, "self" is the name for this required argument.

Related to that, any time you call a method from the same class, you must call it as "self.method". Otherwise, that will return an error as well.

class Flowers:
    def cleaningTask(self):
        pass
 
    def performCleaningTask(self):
        self.cleaningTask()

class petalFlowers(Flowers):
    def cleanThePetals(self):
        print("petals are cleaned")
 
    def cleaningTask(self):
        self.cleanThePetals()
         
class sprayFlowers(Flowers):
    def sprayWater(self):
        print("only water is sprayed")
 
    def cleaningTask(self):
        self.sprayWater()
Reply


Messages In This Thread
Help needed with polymorphism example - by peace - Jan-31-2020, 02:14 PM
RE: Help needed with polymorphism example - by stullis - Jan-31-2020, 03:34 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020