帮助构建对象模型

发布于 2024-12-05 13:50:14 字数 1598 浏览 0 评论 0原文

请帮助我构建对象模型。

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

我的想法如下。

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

每个单位都有不同的计数和不同的防御。构造函数的主体和 GetTotalDefense 的主体始终相同。 我需要是在子类重写Defense中,因为每个单位都有不同。该属性应该是常量,所有坦克实例(士兵,...)都具有相同的防御力。是否有可能继承 const 属性,或者每个孩子都需要自己的 const Defense 属性?

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

哦,还有类 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();
    }
}

另外,请随时提出更好的解决方案,谢谢。

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

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 different. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

才能让你更想念 2024-12-12 13:50:14

您不能真正使用 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);}
   }
}

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);}
   }
}
你的笑 2024-12-12 13:50:14

虽然这个解决方案没有使用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)
    {
    } 
}

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)
    {
    } 
}
嘿嘿嘿 2024-12-12 13:50:14

您正在寻找的实际上是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)
        }
    }
}

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)
        }
    }
}
谁的新欢旧爱 2024-12-12 13:50:14

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

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文