简体   繁体   English

VS2005中的C#:this()会为继承的类执行基础构造函数代码吗?

[英]C# in VS2005: will this() execute the base constructor code for an inherited class?

For C# in VS2005, will calling this() in an inherited class cause the execution of the base constructor? 对于VS2005中的C#,在继承的类中调用this()会导致执行基础构造函数吗?

EDIT: How can I avoid having to re-write the x and y assignments? 编辑:我怎样才能避免重写xy分配? Note, I do not want the MyObject(int num) constructor to execute the base() contructor. 注意,我希望MyObject(int num)构造函数执行base()构造函数。

public class MyObject : MyParentObject { 

int x; 
int y; 
int z; 

public MyObject() { 
    x = 5; 
    y = 10; 
} 

public MyObject(int num) : base(num) { 
    x = 5; 
    y = 10; 
    z = num; 
} 

base() will be called implicitly by the first constructor to run in the derived class: base()将由第一个构造函数隐式调用,以在派生类中运行:

public MyObject() {
    x = 5;
    y = 10;
}

public MyObject(int setZ) : this() {
    z = setZ;
}

is equivalent to: 相当于:

public MyObject() : base() {
    x = 5;
    y = 10;
}

public MyObject(int setZ) : this() {
    z = setZ;
}

The parameterless base constructor will be called implicitly unless you explicitly call a paramterized base constructor. 除非您显式调用参数化的基础构造函数,否则将无提示地调用无参数基本构造函数。

If you have 如果你有

class Base { }
class Foo : Base { public Foo() { } }

It is no different from saying 这与说法没有什么不同

class Foo : Base { public Foo() : base() { } }

So if you have a parameterized constructor for Foo, base() will be called no matter what you do with this() unless you also have a parameterized constructor for Base that you explicitly call. 因此,如果你有一个Foo的参数化构造函数,无论你用this()做什么,都会调用base() this() 除非你还有一个你明确调用的Base的参数化构造函数。

class Base
{
    public Base() { }
    public Base(int bar) { }
}

class Foo : Base
{
    public Foo() : base() { }
    public Foo(int bar) : base(bar) { }
    // also legal: 
    // public Foo() : base(1) { }
    // public Foo(int bar) : base(1) { }
    // public Foo(int bar) : base() { }
    // public Foo(int bar) { } /* uses implicit call to base() */
    // public Foo() { } /* ditto */
}

Either way, the base class will get instantiated first either through the parameterless constructor (implicitly or explicitly) or through the parameterized constructor (explicitly). 无论哪种方式,基类将首先通过无参数构造函数(隐式或显式)或通过参数化构造函数(显式)进行实例化。

I believe this is the syntax you are looking for: 我相信这是您正在寻找的语法:

    class MainClass
{
    MainClass()
    {
        //do something
    }

}

class MyClass : MainClass
{
    MyClass()
        : base()
    {
        // do something else
    }

}

Calling base() will cause it to run the base constructor before the current constructor. 调用base()将使它在当前构造函数之前运行基础构造函数。

class BaseClass
{
   BaseClass()
   {

   }
}

class MyClass : BaseClass
{
   MyClass(int? id) : base()
   {

   }

   MyClass() : this(null)
   {

   }
}

MyClass() : this(null) will call base via MyClass(int? id) MyClass() : this(null)将通过MyClass(int? id)调用base MyClass(int? id)

Or you could swap it around and make MyClass(int? id) : this() and MyClass() : base() either way, the base constructor will be called. 或者你可以交换它并使MyClass(int? id) : this()MyClass() : base()无论哪种方式,都将调用基础构造函数。

The paramater-less constructor (if there is one) is called if nothing is specified, or a compiler error will result (if you only have a base constructor with parameters) 如果没有指定任何内容,则调用无参数的构造函数(如果有的话),否则将产生编译器错误(如果您只有一个带参数的基础构造函数)

You're trying to not instantiate your base class at any point? 你试图不在任何时候实例化你的基类? That's not possible; 那是不可能的; you have to explicitly or implicitly call a base constructor, whether you rewrite the field assigments or not. 无论是否重写字段分配,您都必须显式或隐式地调用基础构造函数。

It's sounds like this isn't the behavior you're looking for (because it calls base() implicitly, but then again, so does your code), but this saves you the rewrite: 听起来这不是你正在寻找的行为(因为它隐式地调用了base() ,但是你的代码也是如此),但这可以节省你的重写:

public class MyObject : MyParentObject { 

int x; 
int y; 
int z; 

public MyObject() { 
    x = 5; 
    y = 10; 
} 

public MyObject(int num) : this() { 
    z = num; 
}

Why specifically do you want to avoid calling the base class constructor? 为什么要特别避免调用基类构造函数?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM