What I am trying to do is to implement a constructor of an Object which is a class.
I have defined the class in the fields of the class; but because it is a class I dont know how to initialize it.
here is my class, and the fields.
public class Player
{
// Instance variables.
//Weapon is a class.
//The max health starts at 30 health points.
private String myPlayerName;
private Weapon myWeapon;
private int myCurrentHealth;
private int myMaxHealth;
private int myNumPotions;
/**
* Constructor initializing class Player
* Parameters of the player should be:
* player name, players initial health, the players weapon.
*/
public Player(String myPlayer, int initialHealth, Weapon currentWeapon) {
myPlayerName = myPlayer;
myWeapon = new Weapon();
myMaxHealth = 30;
initialHealth = myMaxHealth;
myCurrentHealth = initialHealth;
myNumPotions = 0;
}
There is something going wrong here, Im not sure what? Can anyone help me construct the currentWeapon parameter?
Shouldn’t it be this? Why are you constructing a new Weapon object instead of assigning the one passed in the constructor argument?
Update
Construct the Player class this way:-