|
||||||||||||||
. . . . . |
Java Classes
| Intro |
Begin |
if/else |
Loops |
Arrays |
Graphics |
Animation |
Mouse |
Game |
Real |
Methods |
Class |
Class 2 |
Applet |
MouseClick |
Thread |
Button |
A Java class is a reusable entity. It is self-contained and may be thought of as an object like a fish or a rock or book. In a Space Invaders game you might have an alien class, a defender class, a bullet class, a bomb class and a game class that controls the whole thing. If I wanted to write another game that had baddies, i might be able to reuse the aliens and bullet classes. classes can be thought of as modular. They are connected together through methods. Let's make an example to show you :
public class Employee{
private int empNum;
public void setEmpNum(int _x){
bulletx = _x;
}
public int getEmpNum(){
return empNum;
}
}
A class needs an access modifier, the word "class" to signal what it is, and a unique name. Our Employee class
has only one variable - its employee number. It is private so that it cant be hacked into from outside. The only access
to it is through the set and get methods. The set method(setEmpNum) allows other classes to access the
Employee class and change the
empNum variable. The get method(getEmpNum) allows access to what the value of it is.
public class Accountant{
public static void main(String[] args){
// create an instance of class Employee called steve
Employee steve = new Employee();
steve.setEmpNum(234);
System.out.println("Steve : Employee Number :"
+ steve.getEmpNum());
}
}
But what is this line :
Lets write our own Alien class for our game. What do you think it will need ?
public class Baddie{
// declare our variables
private int xpos;
private int ypos;
private Boolean isHit;
// declare our methods
public void setXpos(_x){
xpos = _x;
}
public void setYpos(int _y){
ypos =_y;
}
public void setHit(boolean hitFlag){
isHit = hitFlag;
}
public int getXpos(){
return xpos;
}
public int getYpos(){
return ypos;
}
public boolean getIsHit(){
return isHit;
}
}
So how are we going to use this class? Make a Game class that controls our baddies. Try something like so:
public class Game{
public static void main(String[] args){
String aliveOrDead;
// instantiate a baddie
Baddie pugwort = new Baddie();
pugwort.setXpos(50);
pugwort.setYpos(100);
pugwort.setHit(true);
if(pugwort.getIsHit==true){
aliveOrDead = "Dead";
}else{
aliveOrDead = "Alive";
}
System.out.println("pugwort is at "+
pugwort.getXpos() + " : "
+ pugwort.getYpos() +
" and he is "+ aliveOrDead);
}
}
What did you get ? Where was he ? Was he dead or alive ? What did we do in Game class?
Flash Tutorials in Video Format -
Watch them now at LearnFlash.com Java Class ConstructorsA constructor is something like a method in that has Parentheses . It may be thought of as a method that constructs a class object. You can write your own constructor methods but if you dont Java writes one for you automatically. Called the default constructor. Any constructor you write must have the same name as the class and cannot have a return type.It is usually called in the form of : Object anotherObject = new Object(); // or ClassName instantiatedName = new ClassName(); // e.g. Defender superDork = new Defender(); Lets make a java class constructor for our Employee class. It will have another variable which will be the same for all employees - the salary. Let's do it:
public class Employee{
private int empNum;
private double salary;
// overwrite our default constructor
Employee(){
salary = 300.00;
}
// access methods
public void setEmpNum(int _x){
bulletx = _x;
}
public int getEmpNum(){
return empNum;
}
}
Any employee instantiated will have a default salary of $300.00 . Exercise Next Lesson we will get more into writing constructors and delving deeper into the murky depths of classes. |
|
||||||||||||
|
. | Home | Flash MX | Actionscript 2.0 | Flash 3D | Flash 8 | Flash Database | Flash Mobile | Flash CS3 | Java For Kids | Video Course | General Video | Photoshop | Web Design | Digital Photography | Games | free backgrounds | Resume | Streaming Video | Students Work | Links | Contact me | sitemap | reviews | . . |
||||||||||||||