Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Define methods implementation in base class and properties in derived classes

I am making a game in which I have many kinds of soldiers, each kind with their own attributes (speed, attackPower...). Obviously, all of them can Walk, Attack... so I thought that creating an abstract class Soldier with those methods, and subclasses with each unit attributes would be the appropiate. The problem is that I can't use the attributes of derived classes in the base one.

The easy way would probably be implementing the methods in the derived classes, but that would mean lots of duplicated code, and I want to avoid it. In fact, this would make the base class unneccesary.

I have tried several things. As I understand, the closest solution I tried was using abstract/virtual properties, but again I would be duplicating the "get" code for each unit type. Maybe this can't be avoided, but I'd like to, if possible.

There surely exist a simple solution I haven't thought about. ¿Any ideas?

I think about somethink like this:

public abstract class Soldier {

    public int AttackPower {
        get { return this.power; }
    }

    public Attack {
        Console.WriteLine("Attacked with "+AttackPower+" attack power");
    }
}

public class Lancer:Soldier {

    int power=5;
}

public class Archer:Soldier {

    int power=10;
}

Of course, this is not a correct solution, because the Soldier class doesn't know about the "power" variable, but if I declare the "power" variable in the Soldier class, I get an error because the field name is duplicated.

Any help will be appreciated.

like image 306
David Fernandez Avatar asked May 16 '26 00:05

David Fernandez


2 Answers

You need an abstract property:

public int AttackPower {
        get { return this.power; }
    }

protected abstract int Power { get; }

public class Lancer:Soldier {

    protected override int Power { get { return 5; } }
}

You could also do a "GetPower" method if you really don't like properties. As you've discovered, if a base class method needs access to the data, you have to declare that data in the base class.

Its not code duplication, its type safety!

like image 116
BradleyDotNET Avatar answered May 18 '26 14:05

BradleyDotNET


Why not just put a Power property in the base class?

public abstract class Soldier {
    public int Power {get; set;}

    public int AttackPower {
        get { return this.Power; }
    }

    public Attack {
        Console.WriteLine("Attacked with "+AttackPower+" attack power");
    }
}

public class Lancer:Soldier {
    public Lancer()
    {
        Power = 5
    }
}

public class Archer:Soldier {
    public Archer()
    {
         Power=10;
    }
}

Some design comments:

  • Do you need different classes for Archer and Lancer, or can they just be Soldiers that are configured differently?
  • It would be better to pull property values like this from a data source rather than hard-coding them in the source code. You can embed an XML file or something so it's not easily editable.
like image 35
D Stanley Avatar answered May 18 '26 13:05

D Stanley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!