06.01.2013 Views

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

422 <strong>Learning</strong> <strong>Processing</strong><br />

fill(255);<br />

fill(255,0,255);<br />

fill(0,0,255,150);<br />

Th e ability <strong>to</strong> defi ne functions with the same name (but diff erent arguments) is known as overloading .<br />

With fi ll( ) , for example, <strong>Processing</strong> does not get confused about which defi nition of fi ll( ) <strong>to</strong> look up, it<br />

simply fi nds the one where the arguments match. A function’s name in combination with its arguments<br />

is known as the function’s signature —it is what makes that function defi nition unique. Let’s look at an<br />

example that demonstrates how overloading can be useful.<br />

Let’s say you have a Fish class. Each Fish object has a location: x and y .<br />

class Fish {<br />

float x;<br />

float y;<br />

What if, when you make a Fish object, you sometimes want <strong>to</strong> make one with a random location and<br />

sometimes with a specifi c location. To make this possible, we could write two diff erent construc<strong>to</strong>rs:<br />

Fish() {<br />

x = random(0,width);<br />

y = random(0,height);<br />

}<br />

Fish(float tempX, float tempY) {<br />

x = tempX;<br />

y = tempY;<br />

}<br />

}<br />

When you create a Fish object in the main program, you can do so with either construc<strong>to</strong>r:<br />

Fish fish1 = new Fish();<br />

Fish fish2 = new Fish(100,200);<br />

Overloading allows us <strong>to</strong> defi ne two<br />

construc<strong>to</strong>rs for the same object<br />

(as long as these construc<strong>to</strong>rs take<br />

different arguments).<br />

If two construc<strong>to</strong>rs are defi ned,<br />

an object can be initialized using<br />

either one.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!