Here's an example. When you see something like this
z = x * y;it means that you want the computer to "store the value of x times y in the varibale z, then display z on the screen." As you can see, some programming syntax is borrowed from math. This example includes arithmetic and a function. Functions can also be though of as verbs, with variables as the nouns. In object oriented programming, variables can be nouns which are capable of performing actions. If you had a digital carrier pigeon, and you wanted to tell it to carry a letter to your grandmother's house, you might say something like:
print(z);
myPidgeon.payload = myLetter;In human language, there are always multiple ways to say the same thing, and the same applies in programming. The programmer might just as easily design the program to give grandma the letter like this:
myPideon.flyTo(grandma.house);
myLetter.setRecipient(grandma);Now for some fun. What do the following code snippets mean?
myPideon.deliver(myLetter);
if (jack.getWorkPercent() == 100.0 &&
jack.getPlayPercent() == 0.0) {
jack.dullBoyFlag = true;
}Pie aPie = new Pie();
Song aSong = new Song(sixpence);
aSong.sing();
fill(pocket, rye);
aPie.add(new Blackbird()[24]);Mouse mice[3];
for (i in range(3)) {
mice[i] = new BlindMouse();
}
observe(run(mice));party = new Party(jack, jill);
party.setTarget(water);
party.equip(pail);
party.ascend(hill);
No comments:
Post a Comment