简体   繁体   English

用私有构造函数和私有静态方法调用类?

[英]Calling a class with a private constructor and private static methods?

I created a class with 6 private static methods and a private constructor. 我创建了一个具有6个私有静态方法和一个私有构造函数的类。 The private constructor runs all of the static methods. 私有构造函数运行所有静态方法。 I want to call the class's private constructor in another class, but I'm not able to. 我想在另一个类中调用该类的私有构造函数,但是我不能。 All I want is to run this class once without creating an instance of anything. 我想要的是只运行一次此类而不创建任何实例。 The class populates a small database and I have no need for it other than calling it once. 该类填充一个小型数据库,除了调用它一次,我不需要它。

I could put it into a method, but I don't want to put unrelated code into my main class. 我可以将其放入方法中,但是我不想将无关的代码放入主类中。 I want everything more separated. 我希望一切都分开。 I could just do it with a public constructor and create an instance of the class, but I don't see why I would have to do it that way when an instance isn't needed. 我可以使用公共构造函数来创建类的实例,但是我不明白为什么当不需要实例时必须这样做。

Is there a good way to accomplish what I'm trying to do? 有没有一种好的方法可以完成我想做的事情?

Why not replace your private constructor with a public static method? 为什么不用公共静态方法替换您的私有构造函数?

Your original code: 您的原始代码:

public class DatabaseInitializer
{
    private DatabaseInitializer()
    {
        init1();
        init2();
        ...
    }

    private static void init1() { ... }
    private static void init2() { ... }
    ...
}

Your new code 您的新代码

public class DatabaseInitializer
{
    public static void Init() 
    { 
        init1();
        init2();
        ...
    }

    private static void init1() { ... }
    private static void init2() { ... }
    ...
}

Than you call it: 比您称之为:

Main()
{
    DatabaseInitializer.Init();
}

Singleton would make exact one Instance. 辛格尔顿将提出一个实例。 If you don't want an Instance, just make one static method public. 如果您不希望使用实例,则只需公开一个静态方法即可。 If you want to make sure this is called only once make a static counter or a bool in your class which stop the method from being called a second time call constructor without an instance is impossible, even if it was public 如果要确保仅一次调用此方法,请在您的类中创建一个静态计数器或布尔值,以防止在没有实例的情况下再次调用该方法,即使该方法是公共的,也是如此

I want to call the class's private constructor in another class 我想在另一个类中调用该类的私有构造函数

-- -

All I want is to run this class once without creating an instance of anything 我想要的是只运行一次此类而不创建任何实例

If you do not want to create an instance of your class, then do not use the constructor. 如果您不想创建类的实例,则不要使用构造函数。 I think you just want to use a class to "separate" some code? 我认为您只想使用一个类来“分离”一些代码? Use a static method for that. 为此使用静态方法。

Or if this code should run once and call some static methods. 或者,如果此代码应运行一次并调用一些静态方法。 You can use a static ctor 您可以使用静态ctor

class B
{
  static B() {
    //this stuff called when you create this class or when a static member is referenced

 }
  1. IMHO, this is not an unrelated code for main class. 恕我直言,这不是与主类无关的代码。 It's dependent on these methods. 这取决于这些方法。
  2. You could just have one public method in the class. 您可以在该类中只有一个公共方法。 Dependent classes can then call that method which in turn calls all the private methods doing processing. 然后,从属类可以调用该方法,该方法又调用所有进行处理的私有方法。

If you want to call it or its members from another class, you will need to make a public method. 如果要从另一个类调用它或其成员,则需要创建一个公共方法。

If you don't want any instances of this class around, then you should make a public static method that can be called. 如果您不希望有此类的任何实例,则应该制作一个可以调用的公共静态方法。

The public static method should have a static boolean. 公共静态方法应具有静态布尔值。 When called, it checks that value, and if not set, switches the static boolean value (so it knows it has been called before) and then calls all the private static methods that need to be run once and only once. 调用时,它将检查该值,如果未设置,将切换静态布尔值(因此知道它已经被调用过),然后调用所有需要运行一次且仅运行一次的私有静态方法。

If you only need this initialization done once ever to the database, rather than once per effort, then you should have your static code run out and check the database to see if it has already been initialized. 如果您只需要对数据库执行一次初始化,而不是每次工作一次,那么您应该让静态代码用完并检查数据库,以查看其是否已初始化。 You could make this particularly easy by having a one row table that holds a boolean in it that you can test. 您可以通过拥有一个可以测试的布尔值的单行表来使此操作特别容易。 Just update the initialization code to set that value when the DB is initialized, and have your start up code test for that value to determine its value and what actions to take based upon that value. 只需在初始化DB时更新初始化代码来设置该值,然后让启动代码测试该值以确定其值以及根据该值采取的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM