简体   繁体   English

如何在类中调用构造函数?

[英]How to call constructor inside the class?

I want to call constructor inside the class like: public class Myclass(){ 我想在类中调用构造函数,如:public class Myclass(){

   public MyClass(){
      //......
   }

   public MyClass(int id):this(){
      //......
   }

   private void Reset(){
      //.....
      this = new MyClass(id);    //here I want to call constructor
      //......
   }
}

but it is not working. 但它不起作用。 Is it possible and how can I do it if Yes? 是否可能,如果是,我该怎么办?

Simple answer: You can't. 简单回答:你做不到。

Slightly more complicated answer: Move your initialization logic into a separate method that can be called from the constructor and your Reset() method: 稍微复杂的答案:将初始化逻辑移动到可以从构造函数和Reset()方法调用的单独方法中:

public class MyClass
{
    public int? Id { get; }

    public MyClass()
    {
        Initialize();
    }

    public MyClass(int id)
    {
        Initialize(id);
    }

    public Reset()
    {
        Initialize();
    }

    private Initialize(int? id = null)
    {
        // initialize values here instead of the constructor
        Id = id;
    }
}

You can't. 你不能。 But what you could do is split the constructor logic into an Initialize method that then reset could call. 但是你可以做的是将构造函数逻辑拆分为Initialize方法,然后reset可以调用。

   public MyClass(){
   //......
   }

   public MyClass(int id):this(){
      Initialize(id);
   }

   private void Initialize(int id){
   //....
   }

   private void Reset(){
      //.....
      Initialize(id);
      //......
   }
}

No this is not possible. 不,这是不可能的。 You cannot assign to this. 你不能分配给它。

You could however let Reset() return a new instance of MyClass so the caller code could say: 但是,您可以让Reset()返回MyClass的新实例,以便调用者代码可以说:

public class MyClass
{
    public MyClass Reset()
    {
        return new MyClass();
    }
}

MyClass c = new MyClass();
c = c.Reset();

Or you could implement the Reset method in such a way that all fields are reinitialized to their default values. 或者您可以实现Reset方法,以便将所有字段重新初始化为其默认值。

You can call a constructor for your class inside your class (in fact this is often done with factory methods): 可以在你的类中为你的类调用一个构造函数(实际上这通常用工厂方法完成):

public class MyClass
{
    public static MyClass Create()
    {
        return new MyClass();   
    }
}

But you can't change the value of the this reference inside the class. 但是你不能在类中更改this引用的值。 You should instead have your "Reset()" method set the fields back to their default values. 您应该使用“Reset()”方法将字段设置回默认值。

Within a class you can't reassign this (although it's worth noting that within a struct it's perfectly legal). 在课堂上你不能重新分配this (虽然值得注意的是在一个结构中它是完全合法的)。 The question, though, is whether you should . 但问题是,你是否应该这样做 I assume that your goal is to set all field values to a specific state, so as other people have already said you can either have your Reset method return a new instance or you can break out your logic into a Clear() method and call that instead. 我假设您的目标是将所有字段值设置为特定状态,因此其他人已经说过您可以让您的Reset方法返回一个新实例,或者您可以将您的逻辑分解为Clear()方法并调用该方法代替。

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

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