帮助构建对象模型

时间:2011-09-21 21:55:05

标签: c# oop inheritance abstract-class

请帮我构建对象模型。

我需要抽象类Unit来代表游戏中的每个军事单位。有SoldierTankJetBunker(单位子级)。它们中的每一个都有int个属性CountDefense,带有单个int count参数的构造函数和一个方法GetTotalDefense

我的想法是关注。

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;
}

每个单元都有不同的Count和不同的DefenseGetTotalDefense的构造函数体和正文总是相同的。 我需要的内容在子类覆盖Defense中,因为每个单元都有不同。此属性应为const,Tank(Soldier,...)的所有实例都具有相同的防御。是否有可能继承const属性或每个子需要自己的const Defense属性?

这是我想要实现的一个例子。

哦,还有班级部队

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();
    }
}

此外,随时建议更好的解决方案,谢谢。

PS:我对访问修饰符非常严格,所以在你的例子中要准确,谢谢。

4 个答案:

答案 0 :(得分:3)

你不能真正使用const但是你可以创建一个只读属性你确定你希望这些类是私有的而不是内部的或公共的吗?

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);}
   }
}

答案 1 :(得分:0)

虽然这个解决方案不使用const,但它可以达到你想要的效果:

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
    {
    }
}

或许这更合适:

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)
    {
    } 
}

答案 2 :(得分:0)

您正在寻找的是readonly。此外,由于Defense用于子类,因此需要对其进行保护。

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)
        }
    }
}

答案 3 :(得分:0)

也许是这样的(但这是在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);
    }
}