如何在派生类构造函数之后调用基类方法 [英] How to call base class method after derived class constructor

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

问题描述

各位大家好。

让我知道我有以下课程:



 class ClBase {

public ClBase(){/ * do stuff .. * /}

protected postConstructorMethod(){/ * do do more stuff .. * /}

}

class ClInherited:ClBase {

public ClInherited():base(){/ * do stuff .. * /}

}





有没有办法在派生类构造函数之后调用postConstructorMethod()?



在此先感谢

解决方案

您可以调用基类的任何公共或受保护方法,就像它们是本机方法一样。



如果您有虚方法,可以使用base.Method()语法调用基本版本。你经常在Dispose方法中看到这个。



  protected   override   void  Dispose( bool  disposing)
{
if (!_ dispos)
{
if (处置)
{
// dispose managed resources
base .Dispose( true );
}
}
}


我不太确定你真的需要一个Singleton,或者使用'static:

  public   class  RequireMethodInCtor 
{
public string 名称{ set ; get ; }
public Guid Id { private set < /跨度>; get ; }

public RequireMethodInCtor( string name)
{
Name = name;
Id = Guid.NewGuid();
MethodInCtor( this );
}

受保护 void MethodInCtor(RequireMethodInCtor实例)
{
Console.WriteLine( 新实例:{0} Id:{1},instance.Name,instance.Id.ToString());
}
}

public class Derived1: RequireMethodInCtor
{
public string SomeString {设置; get ; }

// 名称和Id字段将通过调用'base here <来初始化/ span>
public Derived1( string name, string somestring): base (name)
{
SomeString = somestring;
}
}

创建一个新的Derived1实例,并在执行代码之前在代码中放置一个断点:当你点击断点时,单步(F11)和观察会发生什么。



如果绝对必须在基类之后调用一个方法,派生类'ctor执行任何初始代码:< pre lang =cs> public class RequireMethodInCtor
{
public string 名称{ set ; get ; }
public Guid Id { private set < /跨度>; get ; }

public RequireMethodInCtor( string name)
{
Name = name;

Id = Guid.NewGuid();
}

受保护 void MethodInCtor(RequireMethodInCtor实例)
{
Console.WriteLine( 新实例:{0} Id:{1},instance.Name,instance.Id.ToString());
}
}

public class Derived1: RequireMethodInCtor
{
public string SomeString {设置; get ; }

public Derived1( string name, string somestring): base (name)
{
Name = name;
SomeString = somestring;

// 现在调用基本方法
base .MethodInCtor( this );
}
}


Hello everyone.
Let's supose i have the following classes:

class ClBase{

 public ClBase(){/*do stuff..*/}

 protected postConstructorMethod(){ /*do do more stuff..*/}

}

class ClInherited:ClBase{

 public ClInherited():base(){/*do stuff..*/}

}



Is there any way to call "postConstructorMethod()" after the derived class constructor?

Thanks in advance

解决方案

You can call any public or protected method of the base class as if they were native methods.

If you have a virtual method, you can call the base version using the base.Method() syntax. You see this quite often in Dispose methods.

protected override void Dispose(bool disposing)
{
    if(!_disposed)
    {
        if(disposing)
        {
            // dispose managed resources
           base.Dispose(true);
        }
    }
}


I'm not so sure you really need a Singleton here, or the use of 'static:

public class RequireMethodInCtor
{
    public string Name { set; get; }
    public Guid Id { private set; get; }

    public RequireMethodInCtor(string name)
    {
        Name = name;
        Id = Guid.NewGuid();
        MethodInCtor(this);
    }

    protected void MethodInCtor(RequireMethodInCtor instance)
    {
        Console.WriteLine("New instance: {0} Id: {1}", instance.Name, instance.Id.ToString());
    }
}

public class Derived1 : RequireMethodInCtor
{
    public string SomeString { set; get; }

    // Name and Id fields will be initialized by the call to 'base here
    public Derived1(string name, string somestring) : base(name)
    {
        SomeString = somestring;
    }
}

Create a new instance of 'Derived1, and put a breakpoint in your code before it is executed: when you hit the breakpoint, single-step (F11) and observe what happens.

If you absolutely must call a method in the base Class after the derived class 'ctor executes whatever initial code:

public class RequireMethodInCtor
{
    public string Name { set; get; }
    public Guid Id { private set; get; }

    public RequireMethodInCtor(string name)
    {
        Name = name;

        Id = Guid.NewGuid();
    }

    protected void MethodInCtor(RequireMethodInCtor instance)
    {
        Console.WriteLine("New instance: {0} Id: {1}", instance.Name, instance.Id.ToString());
    }
}

public class Derived1 : RequireMethodInCtor
{
    public string SomeString { set; get; }

    public Derived1(string name, string somestring) : base(name)
    {
        Name = name;
        SomeString = somestring;

        // now call the base method
        base.MethodInCtor(this);
    }
}


这篇关于如何在派生类构造函数之后调用基类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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