Created
September 1, 2012 05:38
-
-
Save timsegraves/3564743 to your computer and use it in GitHub Desktop.
Mary Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Mary { | |
// This is a variable storing the current age | |
int age = 0; | |
int weight = 100; | |
// This is a method to increment the current age variable by the amount passed in | |
function increaseAge(int amount) { | |
age = age + amount; | |
} | |
// This is a function to eat food. Capital Food is the class (type) name and food is the instance. | |
// this is probably getting a little ahead of where you are in the book so let me know if you have questions. | |
function eatFood(Food food) { | |
// This is something I just made up. So the food class would define a property called calories. I'm | |
// saying that calories * 1000 is 1 pound. Just made up. | |
weight = weight + food.calories * 1000; | |
} | |
} | |
// Then to use this class: | |
Mary mary = Mary.new(); | |
// mary.weight => would equal 100 | |
// mary.age => would equal 0 | |
mary.increaseAge(5); | |
// mary.age => would equal 5 | |
mary.eatFood(food); // food would be defined somewhere else | |
// mary.weight => would equal 100 + food.calories * 1000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment