在自己的类中调用构造函数

编程入门 行业动态 更新时间:2024-10-26 23:40:34
本文介绍了在自己的类中调用构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

解决方案

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

class Foo { public Foo(string s){...} public Foo(int i) :this(i.ToString()){} //喜欢这个 - > ^^^^^^^^^^^^^^^^^^^^ } Mattias - Mattias Sj?gren [MVP] mattias @ mvps www.msjogren/dotnet/ | www.dotnetinterop 请回复到新闻组。

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 engineeringobjects Matrix.NET的主页 " Nicolas" < NI ***** @ serpe>在留言中写道 news:41 ********************** @ nan-newsreader-07.noos ... 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 www.msjogren/dotnet/ | www.dotnetinterop 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 engineeringobjects Home of Matrix.NET "Nicolas" <ni*****@serpe> wrote in message news:41**********************@nan-newsreader-07.noos...

Mattias Sj?gren wrote:

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

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)

更多推荐

在自己的类中调用构造函数

本文发布于:2023-11-22 07:06:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616434.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自己的   函数   类中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!