Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #176 from Prabhnith/patch-2
Browse files Browse the repository at this point in the history
Example rewritten in Python Mode : Example->Basic->Objects->MultipleCons
  • Loading branch information
Jonathan Feinberg committed Feb 17, 2016
2 parents 386d413 + 3714b08 commit c660b66
Showing 1 changed file with 51 additions and 0 deletions.
@@ -0,0 +1,51 @@
'''
Multiple Constructors
A class can have multiple constructors that assign the fields in different ways.
Sometimes it's beneficial to specify every aspect of an object's data by assigning
parameters to the fields, but other times it might be appropriate to define only
one or a few.
Advanced example written in Python Mode by: Prabhjot Singh (NITH)
Original example in Java Mode: Example->Basic->Objects->MultipleConstructors
'''
def setup():
size(640, 360)
smooth(4)
noLoop()

ellipseMode(CENTER)
strokeWeight(2.5)
stroke(0)
fill('#FFFF00')

global spots
spots = Spot(), Spot(radius=58), Spot(x=120, y=70),\
Spot(width / 2, height / 2, 120)


def draw():
background(0300)
for sp in spots:
sp.display()


class Spot:

def __init__(self, x=None, y=None, radius=40):
if x is None:
self.x = width / 4
else:
self.x = x

if y is None:
self.y = height / 2
else:
self.y = y

self.radius = radius
self.diam = radius * 2


def display(self):
ellipse(self.x, self.y, self.radius, self.diam)

0 comments on commit c660b66

Please sign in to comment.