【发布时间】:2017-03-25 03:52:18
【问题描述】:
我的问题已经在这里被问过了:How to build a group of constant variables in c# 但我觉得好像问的不是同一件事。我会尽力解释,请听我说完。
假设您必须代表一个玩家的属性,例如力量、敏捷、耐力等。但同时您还想列出与同一组不直接相关的其他属性。除了主要属性之外,您还可以拥有概率属性,例如准确度、闪避和暴击。以及后勤属性,例如:运动、主动性和跳跃。自然,这些属性服务于不同的目的,因此,应该单独分组(或者至少在我看来他们应该)。我可以使用多个类来实现这一点:
namespace Example
{
public class PrimaryAttributes
{
public int Strength { get; set; }
public int Agility { get; set; }
public int Stamina { get; set; }
public int Intellect { get; set; }
public int Willpower { get; set; }
public int Spirit { get; set; }
public PrimaryAttributes()
{
// Do stuff here
}
// Do more stuff here
}
}
继续...
namespace Example
{
public class ProbabilisticAttributes
{
public int Accuracy { get; set; }
public int Evasion { get; set; }
public int CriticalStrike { get; set; }
public ProbabilisticAttributes()
{
// Do stuff here
}
// Do more stuff here
}
}
继续...
namespace Example
{
public class LogisticalAttributes
{
public int Movement { get; set; }
public int Initiative { get; set; }
public int Jump { get; set; }
public LogisticalAttributes()
{
// Do stuff here
}
// Do more stuff here
}
}
然后将它们全部托管在一个类中,如下所示:
namespace Example
{
public class Statistics
{
public PrimaryAttributes PrimaryAttributes { get; set; }
public ProbabilisticAttributes ProbabilisticAttributes { get; set; }
public LogisticalAttributes LogisticalAttributes { get; set; }
public LogisticalAttributes()
{
PrimaryAttributes = new PrimaryAttributes();
ProbabilisticAttributes = new ProbabilisticAttributes();
LogisticalAttributes = new LogisticalAttributes();
}
// Do more stuff here
}
}
这将达到能够按照“stats.PrimaryAttributes.Strength”的方式调用某些东西的效果,它干净且有条理。但是,我宁愿不这样做。我想在同一个类中托管所有变量,而不必使用其他类。那么有什么问题呢?我仍然希望能够在扩展范围之后组织它们,或者我猜想什么可以称为命名空间;一种分隔线,如果你愿意的话。我认为在 C# 语言中不存在这样的东西......或任何语言。但我想知道我可以多接近复制这种行为。这是我正在寻找的示例(不是真正的 C#,只是一个示例)。
namespace Example
{
public class Attributes
{
group PrimaryAttributes // Imaginary "group" keyword.
{
int Strength { get; set; }
int Agility { get; set; }
int Stamina { get; set; }
int Intellect { get; set; }
int Wisdom { get; set; }
int Spirit { get; set; }
}
group ProbabilisticAttributes // Imaginary "group" keyword.
{
int Evasion { get; set; }
int Accuracy { get; set; }
int CriticalStrike { get; set; }
}
group LogisticalAttributes // Imaginary "group" keyword.
{
int Movement { get; set; }
int Initiative { get; set; }
int Jump { get; set; }
}
public Attributes()
{
// Don't have to declare or initialize anything.
}
public int ReturnSomethingAmazing()
{
return this.PrimaryAttributes.Strength * this.PrimaryAttributes.Agility; // Notice the group accessor usage.
}
// Do more stuff here
}
}
我希望你能明白我现在在说什么。我希望能够“范围”变量,但不使用单独的类。我还冒昧地思考了如何在编译时对其进行解析。本质上,组范围只是内联,所以myClassInstance.PrimaryAttributes.Strength
,将被最小化为myClassInstance.Strength。范围实际上并不存在,它只是为了让程序员更好地促进组织,而不需要在运行时在内存中消耗更多的对象。
最后,我想总结两个问题。
使用当前的 C# 是否有可能?
和
您认为这是推荐给 Microsoft 的好建议吗? 对于 C# 编程语言的补充?
【问题讨论】:
-
在类中使用区域不会成功吗?或者您可以使用部分类并为每个组使用不同的文件。
-
class组织它们的方法没有任何问题。您可能更喜欢struct,对于他们作为财产袋的目的来说,它有点冗长。我认为您想多了,因为每个类别的class都提供了您所需要的。 -
@Andrew 但是不能通过区域名称访问区域。
-
@Krythic 是的,我明白,我不会关心少数属性的堆分配。如果它们成为您的应用程序性能的瓶颈,那么我会吃掉我的鞋子 :) 再说一次,我认为您将已经有既定模式的东西过度复杂化了。
-
@mariocatch 我不应该提到内存,因为它回避了我的整体观点。我只是想澄清一种可能的用法。我关心的是组织。关于记忆我不能给出两个****,我只是想要组织。
标签: c#