之前用C++,可以在一个类已经实例化之后,再去强行调用自身的构造函数的.class TestCls
{
public:
TestCls(){
cout<<"TestCls Ctor"<<endl;
}
};int main(int argc, char* argv[])
{
TestCls tc;//实例化
tc.TestCls::TestCls();//实例化后再执行构造函数
在C#里面,不知道对应的功能要怎么写?刚刚新学,希望朋友们教一下,谢谢

解决方案 »

  1.   

    TestCls tc;//实例化
        tc.TestCls::TestCls();//实例化后再执行构造函数 
    C#
    TestCls tc = new TestCls();
    tc.TestCls();
      

  2.   

    调用构造函数:自己类调用自己的本来的构造函数Student extends Person{ 
    private String school; Student(String name, String location, String school) { 
    super(name, location); 
    this.school = school; 
    } Student(String name, String school) { 
    this(name, "beijing", school);  
    } public String info { 
     super.info + "school" + school; 


      

  3.   

    1楼的代码试过不行.报错是
    Error 1
    'TestCls' does not contain a definition for 'TestCls' and no extension method 'TestCls' accepting a first argument of type 'TestCls' could be found (are you missing a using directive or an assembly reference?)
    我其实是自己先试过像1楼那样写,不行,再上来问的.先谢谢2楼的代码,不过我是想在类的外面,实例化之后,某实例再显式地调用自己的构造函数.
      

  4.   

    在外部用实例调自己的构造?这个只能变通一下做到吧。
    class TestCls
    {
    public TestCls(){Construct();}
    public void Construct(){...}
    }TestCls tc = new TestCls();
    tc.Construct();
      

  5.   

    楼上妙喻,但调用构造函数不能等同于对象出生.
    可不可以认为 对象的出生 是系统为其分配了内存.构造函数只是初始化一下成员变量.并不实现分配内存的动作.
    那为什么不能再次调用构造函数?是C#不允许这样做,还是.NET Framework 不允许这样做?