[英]I think I need a static class with a constructor?
So, apologies in advance for the surely poor terminology in this question - I'm trying to teach myself C#, and struggling with a few concepts. 因此,对于这个问题中的术语肯定很抱歉,我要事先道歉-我正在尝试自学C#,并为一些概念而苦苦挣扎。
In pseudo-code, I have something like this: 用伪代码,我有这样的东西:
static void Main()
{
// This will create lots of "Item"(s), and do stuff with them
}
public class Item
{
// a bunch of properties
public Item()
{
}
}
Next, I need a UtilityFunction1
that'll do some work - it will be called many times during Main() and passed Item
s to do some calculations with them. 接下来,我需要一个
UtilityFunction1
来完成一些工作-在Main()期间它将多次调用它,并传递Item
来对其进行一些计算。 This function needs to set up a Dictionary
to do its job. 此功能需要设置一个
Dictionary
才能完成其工作。 Even though it will be called many times, the Dictionary should only be set up once. 即使被多次调用,字典也只能设置一次。
I also need a UtilityFunction2
. 我还需要一个
UtilityFunction2
。 Like UtilityFunction1
, this function also needs a Dictionary to do it's job, which should only be set up once. 与
UtilityFunction1
一样,此函数也需要一个Dictionary来完成它的工作,该字典只能设置一次。 This function will be called many times by UtilityFunction1
. UtilityFunction1
将多次调用此函数。
So, on one hand, it seems like UtilityFunction1
and UtilityFunction2
should be public classes, since they need to have a constructor that populates the Dictionary (which involves variables, looping, and calculations). 因此,一方面,看来
UtilityFunction1
和UtilityFunction2
应该是公共类,因为它们需要一个用于填充Dictionary的构造函数(其中涉及变量,循环和计算)。
On the other hand, I only need one of UtilityFunction1
and UtilityFunction2
, so it seems like they should be static methods. 在另一方面,我只需要一个
UtilityFunction1
和UtilityFunction2
,因此它似乎他们应该是静态方法。 But if that's the case, how/when do they do the work to set up the Dictionaries they need? 但是如果是这样,他们如何/何时进行工作以设置所需的字典?
How do you achieve this? 您如何实现的?
You may use a static constructor . 您可以使用静态构造函数 。 A static constructor is guaranteed to be executed just once for the class and before the class is used for the very first time.
保证静态构造函数对于该类仅执行一次,并且在首次使用该类之前即可执行。 A static constructor is a very good place for initialization of static properties.
静态构造函数是初始化静态属性的好地方。 It may look like:
它可能看起来像:
public class ExampleClass
{
public static readonly IDictionary<int, Item> Items;
static ExampleClass()
{
Items = new Dictionary<int, Item>();
}
}
However, be careful with static stuff as it usually decreases the modularity and testability of a program. 但是,请注意静态内容,因为它通常会降低程序的模块化和可测试性。 Therefore the use of a singleton pattern might be a better solution rather than a static constructor.
因此,使用单例模式可能比使用静态构造函数更好。
Maybe you can think of the Singelton Design Pattern for your UtilityFunction* https://msdn.microsoft.com/en-us/library/ff650316.aspx 也许您可以为您的UtilityFunction考虑Singelton设计模式* https://msdn.microsoft.com/zh-cn/library/ff650316.aspx
You need a class that has only one instance, and you need to provide a global point of access to the instance.
您需要一个只有一个实例的类,并且需要提供对该实例的全局访问点。
You could also move your logic outside of Main. 您也可以将逻辑移到Main之外。 What forces you to use static is that Main method (and class it's part of, usually) is static.
迫使您使用静态方法的是Main方法(通常是它的一部分)是静态的。 That forces all other things (fields, methods, events, you name it) to also be static.
这迫使所有其他事物(字段,方法,事件,您将其命名)也都是静态的。 The easiest way of curing that headache would be to create new class to hold all things you need.
解决这种头痛的最简单方法是创建新的类来容纳您需要的所有东西。
Create new Logic class and put all things in it. 创建新的Logic类并将所有内容放入其中。 The Item class stays the same.
Item类保持不变。 you can move it to separate file for clarity.
您可以将其移到单独的文件中以查看清楚。
Logic.cs: Logic.cs:
public class Logic() {
private Dictionary<object,object> dict1 = new Dictionary<object,object>
private Dictionary<object,object> dict2 = new Dictionary<object,object>
public Logic() {
// some logical initializations here
}
private void UtilityFunction1 (Item itemToWorkOn) {
// you can access dict1 directly from here
}
private void UtilityFunction2 (Item itemToWorkOn) {
// you can access dict2 directly from here
}
public void Run() {
// run UtilityFunction1() many times
// run UtilityFunction2() many times
}
}
Item.cs: Item.cs:
public class Item() {
// a bunch of properties
public Item() {}
}
And then just run it in your Main. 然后在您的Main中运行它。
Program.cs: Program.cs:
static class Program {
static void Main() {
new Logic.Run();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.