parm530
10/16/2019 - 12:38 PM

Classes

#
# Example file for working with classes
#


# class declaration
class myClass():
  # self is typically the 1st argument of these instance methods
  # when you call them, you don't need to supply the object
  def method1(self):
    print("myClass method")

  def method2(self, someString):
    print("myClass method2 " + someString)

# To allow inheritance, pass the class as an arg to the child class declaration
class anotherClass(myClass):
  def method1(self):
    # kind of like super in ruby
    # allows your child class to inherit the method 1 of its parent class
    # which prevents you from overriding the parent class' method1
    myClass.method1(self)
    print("anotherClass method1")

  def method2(self, someString):
    # without myClass.method2(self), this method belongs only to this class, sort of like an override
    print("anotherClass method2 " + someString)


def main():

  # instantiate an object instance
  c = myClass()
  # no need to supply the 'self'
  c.method1()
  c.method2("Hello world!")

  c2 = anotherClass()
  c2.method1()
  c2.method2("This is a string")

  
if __name__ == "__main__":
  main()