在自己的类中调用构造函数 [英] Calling a constructor in its own class

查看:101
本文介绍了在自己的类中调用构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




如何在构造函数本身的同一个类中调用构造函数?


谢谢。

解决方案

如何在与构造函数本身相同的类中调用构造函数?




class Foo

{

public Foo(string s){...}

public Foo(int i) :this(i.ToString()){}

//喜欢这个 - > ^^^^^^^^^^^^^^^^^^^^

}


Mattias


-

Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com

请回复到新闻组。


Mattias Sj?gren写道:

如何在同一个构建函数中调用class作为构造函数本身?



class Foo
公共Foo(字符串s){...}
公共Foo(int i) :this(i.ToString()){}
//像这样 - > ^^^^^^^^^^^^^^^^^^^^
}




我很抱歉赢了''解决我的问题,因为我需要做的是

从同一个类的另一个方法调用构造函数(而不是来自

同一类的另一个构造函数)


如果你的意思是


C c1 = new C();

C c2 = c1 .Clone();


然后Clone看起来像


public C Clone(){

C结果=新C();

返回结果;

}

ottomh

-

Grace + Peace,

Peter N Roth

Engineering Objects International
http://engineeringobjects.com

Matrix.NET的主页

" Nicolas" < NI ***** @ serpe.org>在留言中写道

news:41 ********************** @ nan-newsreader-07.noos.net ... < blockquote class =post_quotes> Mattias Sj?gren写道:

如何在与构造函数本身相同的类中调用构造函数?



class Foo
{public Foo(string s){...}
public Foo(int i):this(i.ToString() ){}
//喜欢这个 - > ^^^^^^^^^^^^^^^^^^^^
}



对不起,我不能解决我的问题,因为我需要做的是从同一个类的另一个方法调用构造函数(而不是从同一个类的另一个
构造函数)



Hi,

How can one call a constructor in the same class as the constructor itself ?

Thanks.

解决方案

How can one call a constructor in the same class as the constructor itself ?



class Foo
{
public Foo(string s) { ... }
public Foo(int i) : this(i.ToString()) {}
// like this -> ^^^^^^^^^^^^^^^^^^^^
}

Mattias

--
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Mattias Sj?gren wrote:

How can one call a constructor in the same class as the constructor itself ?


class Foo
{
public Foo(string s) { ... }
public Foo(int i) : this(i.ToString()) {}
// like this -> ^^^^^^^^^^^^^^^^^^^^
}



I''m sorry that won''t solve my problem, because what I need to do is to
call a constructor from another method of the same class (not from
another constructor of the same class)


if you mean like

C c1 = new C();
C c2 = c1.Clone();

then Clone will look like

public C Clone() {
C result = new C();
return result;
}
ottomh
--
Grace + Peace,
Peter N Roth
Engineering Objects International
http://engineeringobjects.com
Home of Matrix.NET
"Nicolas" <ni*****@serpe.org> wrote in message
news:41**********************@nan-newsreader-07.noos.net...

Mattias Sj?gren wrote:

How can one call a constructor in the same class as the constructor
itself ?


class Foo
{
public Foo(string s) { ... }
public Foo(int i) : this(i.ToString()) {}
// like this -> ^^^^^^^^^^^^^^^^^^^^
}



I''m sorry that won''t solve my problem, because what I need to do is to
call a constructor from another method of the same class (not from another
constructor of the same class)



这篇关于在自己的类中调用构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆