繁体   English   中英

从子类c#调用父方法

[英]Call parent method from child class c#

这是一个与我之前看到的答案略有不同的问题,或者我没有得到它。 我有一个父类,有一个名为MyMethod()的方法和一个变量public Int32 CurrentRow;

public void MyMethod()
{    
     this.UpdateProgressBar();    
}

在父级中,我创建了一个ChildClass的新实例

Boolean loadData = true;
if (loadData) 
{    
     ChildClass childClass = new ChildClass();    
     childClass.LoadData(this.Datatable);    
}

在子类LoadData()方法中,我希望能够设置父类的CurrentRow变量并调用MyMethod()函数。

我该怎么做呢?

要访问父类的属性和方法,请使用base关键字。 所以在你的子类LoadData()方法中你会这样做:

public class Child : Parent 
{
    public void LoadData() 
    {
        base.MyMethod(); // call method of parent class
        base.CurrentRow = 1; // set property of parent class
        // other stuff...
    }
}

请注意,您还必须将父MyMethod()的访问修饰符更改为至少protected ,以便子类访问它。

找到了解决方案。

在父类中,我声明了一个ChildClass()的新实例,然后将该类中的事件处理程序绑定到父类中的本地方法

在子类中,我添加了一个公共事件处理程序:

public EventHandler UpdateProgress;

在父级中,我创建此子类的新实例,然后将本地父事件绑定到子级中的public eventhandler

ChildClass child = new ChildClass();
child.UpdateProgress += this.MyMethod;
child.LoadData(this.MyDataTable);

然后在我可以调用的子类的LoadData()

private LoadData() {
    this.OnMyMethod();
}

OnMyMethod在哪里:

public void OnMyMethod()
{
     // has the event handler been assigned?
     if (this.UpdateProgress!= null)
     {
         // raise the event
         this.UpdateProgress(this, new EventArgs());
     }
}

这将在父类中运行该事件

跟进suhendri对Rory McCrossan的评论回答。 这是一个Action委托示例:

在孩子添加:

public Action UpdateProgress;  // In place of event handler declaration
                               // declare an Action delegate
.
.
.
private LoadData() {
    this.UpdateProgress();    // call to Action delegate - MyMethod in
                              // parent
}

在父级中添加:

// The 3 lines in the parent becomes:
ChildClass child = new ChildClass();
child.UpdateProgress = this.MyMethod;  // assigns MyMethod to child delegate

一种方法是在构造时将ParentClass的实例传递给ChildClass

public ChildClass
{
    private ParentClass parent;

    public ChildClass(ParentClass parent)
    {
        this.parent = parent;
    }

    public void LoadData(DateTable dt)
    {
       // do something
       parent.CurrentRow++; // or whatever.
       parent.UpdateProgressBar(); // Call the method
    }
}

在父内部构造ChildClass时,确保传递this的引用:

if(loadData){

     ChildClass childClass = new ChildClass(this); // here

     childClass.LoadData(this.Datatable);

}

警告:这可能不是组织课程的最佳方式,但它直接回答了你的问题。

编辑 :在评论中你提到超过1个父类想要使用ChildClass 这可以通过引入接口来实现,例如:

public interface IParentClass
{
    void UpdateProgressBar();
    int CurrentRow{get; set;}
}

现在,确保在两个(所有?)父类上实现该接口,并将子类更改为:

public ChildClass
{
    private IParentClass parent;

    public ChildClass(IParentClass parent)
    {
        this.parent = parent;
    }

    public void LoadData(DateTable dt)
    {
       // do something
       parent.CurrentRow++; // or whatever.
       parent.UpdateProgressBar(); // Call the method
    }
}

现在,任何实现IParentClass可以构建的一个实例ChildClass ,并通过this给它的构造。

暂无
暂无

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

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