我可以从一个构造函数调用另一个构造函数吗? [英] can I call from one constructor another constructor ?

查看:72
本文介绍了我可以从一个构造函数调用另一个构造函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

公共班级A

{

public A()

{

//这里我想要要调用_ctor的第二个版本,如何实现这个


}


public A(int a,int b ,int c)

{

//一些代码

}

}

感谢您的任何建议,

Paul

public class A
{
public A ()
{
// here I would like to call the second version of _ctor, how to
accomplish this ?
}

public A (int a, int b, int c)
{
// some code
}
}

Thanks for any advice,
Paul

推荐答案

你做不到。你必须将第二个构造函数中的代码移动到一个名为''Initialize''的

函数中,然后从两个构造函数中调用该函数

。 br />

但是,您可以使用base关键字在构造函数签名中调用继承的构造函数

,例如:


public A(int param1,string param2):base(param1,param2)

{

...

}
< br $>
-

John Wood

电子邮件:名字,点,姓氏,来自priorganize.com


保罗 <无***** @ dupa.com>在消息中写道

news:u3 ************* @ tk2msftngp13.phx.gbl ...
You cannot. You will have to move the code in the second constructor into a
function, called ''Initialize'' for example, and then invoke that function
from both constructors.

You can, however, invoke inherited contructors in the constructor signature
using the base keyword, eg:

public A (int param1, string param2) : base(param1, param2)
{
...
}

--
John Wood
EMail: first name, dot, last name, at priorganize.com

"Paul" <no*****@dupa.com> wrote in message
news:u3*************@tk2msftngp13.phx.gbl...
公共类A {
公众A()
//
//这里我想打电话给_ctor的第二个版本,如何实现这个目标?
}
公共A(int a,int b,int c)
{
//一些代码
}
}

感谢任何建议,
Paul
public class A
{
public A ()
{
// here I would like to call the second version of _ctor, how to
accomplish this ?
}

public A (int a, int b, int c)
{
// some code
}
}

Thanks for any advice,
Paul



Paul。


用它来引用当前例如。


例如


公共A():这(1,2,3){

。 ...

}


HTH

Alex


" Paul" <无***** @ dupa.com>在消息中写道

news:u3 ************* @ tk2msftngp13.phx.gbl ...
Paul.

use this to reference current instance.

E.g.

public A() : this(1,2,3) {
....
}

HTH
Alex

"Paul" <no*****@dupa.com> wrote in message
news:u3*************@tk2msftngp13.phx.gbl...
公共类A {
公众A()
//
//这里我想打电话给_ctor的第二个版本,如何实现这个目标?
}
公共A(int a,int b,int c)
{
//一些代码
}
}

感谢任何建议,
保罗
public class A
{
public A ()
{
// here I would like to call the second version of _ctor, how to
accomplish this ?
}

public A (int a, int b, int c)
{
// some code
}
}

Thanks for any advice,
Paul



这样做:

public A():this (1,2,3)

{

//其他构造函数将在任何事情之前被调用

//在这个中发生。

}


克里斯


" Paul" <无***** @ dupa.com>在消息中写道

news:u3 ************* @ tk2msftngp13.phx.gbl ...
do it like this:
public A() : this(1, 2, 3)
{
// other constructor will be called before anything
// happens in this one.
}

Chris

"Paul" <no*****@dupa.com> wrote in message
news:u3*************@tk2msftngp13.phx.gbl...
公共类A {
公众A()
//
//这里我想打电话给_ctor的第二个版本,如何实现这个目标?
}
公共A(int a,int b,int c)
{
//一些代码
}
}

感谢任何建议,
Paul
public class A
{
public A ()
{
// here I would like to call the second version of _ctor, how to
accomplish this ?
}

public A (int a, int b, int c)
{
// some code
}
}

Thanks for any advice,
Paul



这篇关于我可以从一个构造函数调用另一个构造函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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