So I've been muddling through Java by myself for a while with various projects (self teaching via play - the best way to learn) and I've currently got this little code snippet which I'm using to overwrite one 'skill' with a new one. It works fine in most cases, but there's a specific case where I get a different version.
This is the code for the method I'm investigating:
public void copy(Skill oldSkill) {
if (oldSkill != null){
this.name = oldSkill.getName();
this.level = 0;
this.rank = SkillRank.DABBLER ;
this.difficulty = oldSkill.getDifficulty();
if (oldSkill.neededSkill1 != null) {
this.neededSkill1 = oldSkill.neededSkill1;
if (oldSkill.neededSkill2 != null) {
this.neededSkill2 = oldSkill.neededSkill2;
if (oldSkill.neededSkill3 != null) {
this.neededSkill3 = oldSkill.neededSkill3;
}
}
}
}
else {
this.name = "default";
this.level = 0;
this.rank = SkillRank.DABBLER ;
this.difficulty = Difficulty.BASIC;
}
}
This is the code of the minitest I've made (I have used Delta to avoid null pointers for now):
System.out.println("\nCopy Test:");
Skill Delta = new Skill("Delta", Difficulty.BASIC);
Skill GammaCopy = Delta;
GammaCopy.copy(Gamma);
System.out.println("New Skill name (Gamma): "+GammaCopy.name );
System.out.println("New skill Difficulty (Basic): "+GammaCopy.difficulty.name() );
System.out.println("New skill level (0): "+GammaCopy.level);
System.out.println("New Skill Gama preReqs (Alpha, Beta, Theta): "+Gamma.neededSkill1.name +", "+Gamma.neededSkill2.name+", "+Gamma.neededSkill3.name );
System.out.println("New Skill GamaCopy preReqs (Alpha, Beta, Theta): "+GammaCopy.neededSkill1.name +", "+GammaCopy.neededSkill2.name+", "+GammaCopy.neededSkill3.name );
This is the output I would expect on my minitest (and get except for in one particular case):
Copy Test:
New Skill name (Gamma): Gamma
New skill Difficulty (Basic): BASIC
New skill level (0): 0
New Skill Gama preReqs (Alpha, Beta, Theta): Alpha, Beta, Theta
New Skill GamaCopy preReqs (Alpha, Beta, Theta): Alpha, Beta, Theta
The case where it all goes funny is if I replace 'Delta' with a previously initialised skill called 'Alpha' (or Beta or Theta). The new Skill print output is as follows:
New Skill Gama preReqs (Alpha, Beta, Theta): Gamma, Beta, Theta
New Skill GamaCopy preReqs (Alpha, Beta, Theta): Gamma, Beta, Theta
I have no idea why. I've tried closing the file and the output window and redoing it. This didn't fix it (didn't really expect it to) so its not a stored variable thing like you might sometimes get with Matlab...
Can anyone explain why this is happening?
Aucun commentaire:
Enregistrer un commentaire