开发者

Help with building object model

Help me with building object model, please.

I need abstract class Unit representing each military unit in a game. There is Soldier, Tank, Jet and Bunker (children of Unit). Each of them has int properties Count and Defense, constructor with single int count parameter and one method GetTotalDefense.

My idea is following.

private abstract class Unit
{
    private int Count { get; set; }
    private const int Defense = 0;

    protected Unit(int count)
    {
        Count = count;
    }

    public int GetTotalDefense()
    {
        return Count * Defense;
    }
}

private class Tank : Unit
{
    private const int Defense = 5;
}

Each unit has different Count and different Defense. Body of constructor and body of GetTotalDefense is always the same. What I need is in child class override Defense, because each unit has diffe开发者_如何学Crent. This property should be const, all instances of Tank (Soldier, ...) has same defense. Is there a possibility to inherit const property or each child needs its own const Defense property?

And here is an example I'd like to achieve.

Oh, there is also class Troop

public class Troop
{
    private Soldier Soldiers { get; set; }
    private Tank Tanks { get; set; }
    private Jet Jets { get; set; }
    private Fort Forts { get; set; }

    public Troop(int soldiers, int tanks, int jets, int forts)
    {
        Soldiers = new Soldier(soldiers);
        Tanks = new Tank(tanks);
        Jets = new Jet(jets);
        Forts = new Fort(forts);
    }

    public int GetTotalDefense()
    {
        return Soldiers.GetTotalDefense() + Tanks.GetTotalDefense() + Jets.GetTotalDefense() + Forts.GetTotalDefense();
    }
}

Also, feel free to suggest better solution, thanks.

PS: I'm really strict about access modifiers, so be precise in your examples, thank you.


You can't really use a const but you can make a readonly property also are you sure you want the classes to be private and not internal or public?

public abstract class Unit { 

    protected Unit(int count) {
      Count=count;
    }

    protected int Count { get; private set; }
    protected abstract int Defense {get;}

    public int TotalDefense {
      get { return Count*Defense; }
    }

}

public class Tank : Unit {

   public Tank(int count) : base(count) {}

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

public class Troop {

   private Unit[] Troops;

   public Troop(int soldiers, int tanks, int jets, int forts) {
     Troops = new Unit[] {
                new Soldier(soldiers),
                new Tank(tanks),
                new Jet(jets),
                new Fort(forts)
              };
   }

   // The using System.Linq you can do
   public int TotalDefense {
     get { return Troops.Sum(x=>x.TotalDefense);}
   }
}


Although this solution does not use const, it achieves what you want:

internal abstract class Unit
{
    private int Count { get; set; }
    private int Defense { get; set; }
    public int TotalDefense { get { return Count * Defense; } }

    protected Unit(int defense, int count)
    {
        Defense = defense;
        Count = count;
    }
}

internal class Tank : Unit
{
    protected Tank(int count)
        : base(5, count)  // you can use a const variable instead of 5
    {
    }
}

Or maybe this is more suitable:

internal abstract class Unit 
{ 
    private int Count { get; set; } 
    public abstract int Defense { get; }
    public int TotalDefense { get { return Count * Defense; } }

    protected Unit(int count) 
    { 
        Count = count;
    }
} 

internal class Tank : Unit 
{
    override public int Defense { get { return 5; } }

    protected Tank(int count) : base(count)
    {
    } 
}


What you're looking for is actually readonly. Also, since Defense is used in subclasses, you need to make it protected.

private abstract class Unit  
{  
    private int _Count;  
    protected readonly const int Defense;

    public int TotalDefense
    { get { return Count * Defense; } }

    protected Unit (int count, int defense)
    {
        Defense = defense;
        _Count = count;
    }
}  

private class Tank : Unit  
{
    public Tank (int Count)
        : base (Count, 5)
    { }        
}

public class Troop
{
    public IEnumerable<Unit> Units { get; protected set; }

    public Troop (int soldiers, int tanks, int jets, int forts)
    {
        Troops = new Unit[]
        {
            new Soldier (soldiers),
            new Tank (tanks),
            new Jet (jets),
            new Fort (forts)
        }
    }
}


maybe something like this (but this is in java)

abstract class Unit {
    Unit(int defense,int count) {
        this.defense = defense;
        this.count=count;
    }
    final int defense;
    int count;
}
class Soldier extends Unit {
    Soldier(int count) {
        super(1,count);
    }
}
class Tank extends Unit {
    Tank(int count) {
        super(5,count);
    }
}
public class Main {
    public static void main(String[] args) {
        Unit[] units = { new Soldier(2), new Tank(3) };
        for(Unit unit:units)
            System.out.println(unit.count+" "+unit.defense);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜