|
Home <<Table of Contents <<Loops More on the Class>> |
One of the main goals behind Java was to achieve an entirely object-oriented programming language. The concept behind object-oriented programmiong (OOP) is part of Java in the form of classes and methods. This chapter is a guide to understanding the use of objects in Java and their implementation. | |||||||
|
What are Classes?
| ||||||||
|
Using Classes
You can replace Classname with the name of your class. This, actually looks very similar to one of the beginning statements of the Hello World program (see the first chapter: "Introduction to Java"). In fact, the hello world program actually is a class. The one noteable difference is the "extends Applet" part which makes the class an applet. Once you create a class, you've wasted your time unless you execute the code in the class. One way to do this is to embed you class into an web page with the applet tag, but that is only for applets. You can also have your class declared as a variable in another class. Here is an example of a class named "Sprite" declared as a variable,
Now, you have a cotainer for a Sprite class, but it is empty. This is like having an integer variable with no number in it. You would put a number in it with a statement like "MyInt = 5;" and this is similar for classes. To fill a variable with a new class, you must use the "new" operator. Here is an example.
This puts a new sprite class in the variable "MySprite." When you have a class in a variable, you can call the methods of the class. | ||||||||
|
What are methods?
In this lovely declaration, Draw is the name of the method, and the word "public" serves the same function as it did in the class declaration. The braces ({}), as with the braces in classes, show where the method begins and ends. Some other parts of the method decleration have gone unexplained so far, but they are explained later. If you wanted to draw your sprite, you would just use this code.
However, this will only work if it is called inside of your sprite class. Otherwise, the computer will have no idea where to find the sprite class. This is where variables filled with classes come into play. Let us say you have a variable named MySprite with a new Sprite class in it; you can call a method with this variable. To do this, you use the dot operator (a period) on the variable. Here is an example.
This is an example of how you can control the flow of your program with classes and methods. Other parts of the class and method will allow you to gain even more control over your program's flow. | ||||||||
|
Parameters
The "Power" is a parameter that must be passed to the method when it is called. Here is an example.
This will call the "Attack" method and give the "Power" parameter a value of five. If you wanted your goblin to attack again with a power of only four, you would just use the same command, but substitute "4." You may have noticed that "int" has popped up again. When declaring methods, all parameters must have a data type. Passing a parameter that does not fit into the data type of the parameter causes an error. The statement "Attack(5.6);" would cause an error because 5.6 is not an integer. You can also have subs with multiple parameters. Here is an example.
To attack, you now must call the method like this.
You may use any numbers you want for the parameters, as long as they are within the specifications for their data-types.
You would call your method like this.
This method will then create a variable named "c" and put in it the sum of "a" and "b." In this case, it creates "c" and makes it equal to 5 + 6. | ||||||||
|
Casting
The two variables (Num1 and Num2) will be converted to integers. In the process, they will be rounded down to whole numbers. | ||||||||
|
Overloading
If you only specify one parameter, Java calls the first method, but if you specify two parameters, Java calls the second method. However, if you specify one floating point parameter, such as "6.3," the compiler will give you an error. | ||||||||
|
Return Values
The "return" statement tells the method what to return. Now the method acts as a value of the specified type such as "5." You can set other variables to be equal to the return value like this.
Now, "MyVar" eqauls the sum of 5 and 8, or 13. You may also use a statement like this.
This calls the method add, returns 13 (the sum of 5 and 8), multiplies it by 6 and sets MyVar equal to the result 78. |
Home <<Table of Contents <<Loops More on the Class>> |
Now that you have an understanding of object-oriented programming, you can learn more about the class in the next chapter, but first, you might want to go grab another snack. |