This site requires JavaScript, please enable it in your browser!
Greenfoot back
FirewolfTheBrave
FirewolfTheBrave wrote ...

2021/1/20

Call super() at a later point?

FirewolfTheBrave FirewolfTheBrave

2021/1/20

#
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
this.weapon = new Weapon ("shortsword");
Then the Weapon constructor would have to look like this:
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;
        }
    }
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,
    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;
        }
    }
there will be a problem because super() has to be the first statement on the constructor. Is there a simple solution for this?
danpost danpost

2021/1/20

#
FirewolfTheBrave wrote...
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 << Code Omiitted >> Then the Weapon constructor would have to look like this: << Code Omitted >> 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, << Code Omitted >> there will be a problem because super() has to be the first statement on the constructor. Is there a simple solution for this?
It helps to be organized:
static final int SHORT_SWORD = 0, LONG_SWORD = 1, GREAT_SWORD = 2;
static final int[] PRICES= { 50, 75, 100 );
static final int[] ATTACK_MIN = { 5, 7, 9 };
static final int[] ATTACK_MAX = { 8, 11, 13 };

int type;

public Weapon(int pType)
{
    super( PRICES[pType] );
    type = pType;
}
To create a weapon:
Weapon weapon = new Weapon(Weapon.SHORT_SWORD);
FirewolfTheBrave FirewolfTheBrave

2021/1/20

#
Thank you, that works!
You need to login to post a reply.