帮助构建对象模型
请帮助我构建对象模型。
我需要代表游戏中每个军事单位的抽象类 Unit
。有 Soldier
、Tank
、Jet
和 Bunker
(Unit 的子级)。它们每个都有 int
属性 Count
和 Defense
,具有单个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不能真正使用 const,但您可以创建只读属性,并且您确定希望这些类是私有的而不是内部或公共的吗?
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?
虽然这个解决方案没有使用const,但它实现了你想要的:
或者也许这更合适:
Although this solution does not use const, it achieves what you want:
Or maybe this is more suitable:
您正在寻找的实际上是
readonly
。另外,由于Defense
在子类中使用,因此您需要将其设置为受保护的。What you're looking for is actually
readonly
. Also, sinceDefense
is used in subclasses, you need to make it protected.也许是这样的(但这是在java中)
maybe something like this (but this is in java)