Classes and Objects 2

Today continues on the basics of classes and objects.

Reading

Re-read Chapter 14 up to and including "Classes as Types".

Today in Class

  1. Exercises on creating basic classes
  2. The 'self' variable
  3. When you need __init__() and when you do not (hint: almost always)

You should be able to understand this program.

class Midshipman:
    def __init__(self, name, alpha, company, oom=1000):
        self.name = name
        self.alpha = alpha
        self.co = company
        self.oom = oom

    def march(self):
        print(self.name, 'is march march marching.')

    def switch_company(self, newco):
        self.co = newco

    def increment_oom(self, amt):
        self.oom += amt

mid1 = Midshipman('Chambers', 203452, 23, 50)
mid2 = Midshipman('Herron', 208743, 14, 950)

mid1.march()
mid2.march()
mid2.switch_company(mid1.co)

print(mid1.name, 'is in', mid1.co, 'company')
print(mid2.name, 'is in', mid2.co, 'company')