squishysquashy
12-04-2011, 08:54 PM
Hello everyone, please help me out, I'm tearing my hair out!
I have three classes; Human, Player and Hunter.
The Human has attributes x, y and state, plus various methods, and I want the Player and Hunter to inherit these attributes and methods.
My program prints a grid with the positions of the Player and the Hunters (x and y being their respective co-ordinates). Is there any way I can have the Player and Hunter extend Human but retain separate attributes?
For example, I create a Player object and a Hunter object. Right now, x = 0 and y = 0 for them both. If I move the Player to the right, x = 1 for them both, but I only want x = 1 for the Player, I still want x = 0 for the Hunter. Does that make sense?
Here is the relevant code:
Player player = new Player(); // create Player
Hunter[] hunter = new Hunter[5]; // allocates memory for 5 Hunters
for (int i = 0; i < 5; i++) {
hunter[i] = new Hunter();
}
public class Human
{
private int x;
private int y;
private int state;
}
public class Player extends Human
{
}
public class Hunter extends Human
{
}
etc.
So what can I change to have different x and y co-ordinates for each instance of each class?
Thanks in advance!
I have three classes; Human, Player and Hunter.
The Human has attributes x, y and state, plus various methods, and I want the Player and Hunter to inherit these attributes and methods.
My program prints a grid with the positions of the Player and the Hunters (x and y being their respective co-ordinates). Is there any way I can have the Player and Hunter extend Human but retain separate attributes?
For example, I create a Player object and a Hunter object. Right now, x = 0 and y = 0 for them both. If I move the Player to the right, x = 1 for them both, but I only want x = 1 for the Player, I still want x = 0 for the Hunter. Does that make sense?
Here is the relevant code:
Player player = new Player(); // create Player
Hunter[] hunter = new Hunter[5]; // allocates memory for 5 Hunters
for (int i = 0; i < 5; i++) {
hunter[i] = new Hunter();
}
public class Human
{
private int x;
private int y;
private int state;
}
public class Player extends Human
{
}
public class Hunter extends Human
{
}
etc.
So what can I change to have different x and y co-ordinates for each instance of each class?
Thanks in advance!