开发者

How to call another ctor?

开发者 https://www.devze.com 2023-03-03 15:59 出处:网络
For example, in my class Foo has two ctor methods, how do I call parameterless ctor in another ctor? class Foo {

For example, in my class Foo has two ctor methods, how do I call parameterless ctor in another ctor?

class Foo {
   public Foo() {
      // initialized this class 
   }

   public Foo(int a, int b) {
      // i开发者_StackOverflownitialized by Foo(), how do I call Foo() here ?

      .... // other initializing here
   }

}


Add : this() between the parameter list and the opening brace:

class Foo {
   public Foo() {
   }

   public Foo(int a, int b) : this() {
   }
}


Put it in the initializer list, like so:

public Foo(int a, int b) : this() {
          // initialized by Foo(), how do I call Foo() here ?

          .... // other initializing here
       }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号