Well, let's get started.
Now then, classes that have a need of objects...this is what we are going to make.
I'll start with an example that some people like, a Character (for a video game) class. Let's start with the basic header for our class and a constructor.
class Character
{
public Character()
{
}
}
Right now, what's in that code is the class construct itself and the constructor. Notice how the constructor has the EXACT same name as the class. This is important. If it doesn't have the same name, it's not a constructor.
Well, this is not a very good class yet, since we don't have any data...or methods...or anything besides a constructor...so let's add some! A Character needs HP and MP, right?
class Character
{
private int HP;
private int MP;
public Character()
{
}
}
We declare these global to the class itself, so that any entity inside the class can access them. Notice how we declare these private. Declaring them private makes it so that they can only be accessed and/or changed by members of it's own class (AKA the methods). If we had declared these public, they would be directly accessible outside of the class, and not just through the methods of the class.
Well, now we have data, but it's never initialized, let's add that real quick.
class Character
{
private int HP;
private int MP;
public Character(int hp, int mp)
{
this.HP = hp;
this.MP = mp;
}
}
One change that might not be obvious here is that the parameters of the constructor changed. The parameters are what's inside the parentheses. These are passed to the constructor when we use it. It needs to know these values so that it can assign values accordingly.
Notice the this keyword. This is a pointer to the object that is calling the method. If we called it like this -- OBJECT.method();, in that case, the this keyword will reference OBJECT. It just represents the current object and all of its data.
Now we have some HP and MP. Hooray! But now we need some methods to access these variables, right? RIGHT, Locke!
class Character
{
private int HP;
private int MP;
public Character(int hp, int mp)
{
this.HP = hp;
this.MP = mp;
}
public int getHP()
{
return this.HP;
}
public int getMP()
{
return this.MP;
}
}
Well, now we have some methods to access our attributes. Notice the return type before the method heading...it NEEDS to be there, so that the method knows what it's supposed to be returning. The return type is always the same as the variable type in basic return methods like these, that just access and tell you what the variable value is.
So if we have something like this...
private String name;
public String getName()
{
return this.name;
}
Well, with our private declaration, we can't change our HP or MP values, only access them with our methods, so let's add some set methods.
class Character
{
private int HP;
private int MP;
public Character(int hp, int mp)
{
this.HP = hp;
this.MP = mp;
}
public int getHP()
{
return this.HP;
}
public int getMP()
{
return this.MP;
}
public void setHP(int newHP)
{
this.HP = newHP;
}
public void setMP(int newMP)
{
this.MP = newMP;
}
}
We MUST have parameters in our set methods, or Java doesn't know what to set the values to. Notice the void keyword in the method heading...this means the method doesn't return anything. It doesn't need to, since it's just changing values.
SUM UP
To create a new object of our Character class:
Character locke = new Character(50, 50); // locke is the variable name // starts out with 50 HP and 50 MP
To retrieve values:
int lockesHP = locke.getHP(); int lockesMP = locke.getMP();
To set values:
// set locke's HP to 100 locke.setHP(100); // set locke's MP to 55 locke.setMP(55);
______________________________________
Well, this is just a very basic tutorial in designing Object methods/classes. If you want to know how to make classes that don't require instances to function, you can look at my other tutorial here.
Bye!






MultiQuote






|