In my current project, there are multiple types of equipment, divided into the subclasses Weapon and Armor. I want to be able to simply assign a piece of equipment to a character by writing
Then the Weapon constructor would have to look like this:
However, there is a third attribute, price, which is stored in the superclass Equipment. I could pass it down through the Equipment constructor, but if I code it like this,
there will be a problem because super() has to be the first statement on the constructor. Is there a simple solution for this?
this.weapon = new Weapon ("shortsword");
public Weapon (String pType) { this.type = pType; switch (type) { case "shortsword": this.attackMin = 5; this.attackMax = 8; break; case "longsword": this.attackMin = 7; this.attackMax = 11; break; case "greatsword": this.attackMin = 9; this.attackMax = 13; break; } }
public Weapon (String pType) { this.type = pType; switch (type) { case "shortsword": this.attackMin = 5; this.attackMax = 8; super(50); break; case "longsword": this.attackMin = 7; this.attackMax = 11; super(75); break; case "greatsword": this.attackMin = 9; this.attackMax = 13; super(100); break; } }