从子类 C# 调用父方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13743609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call parent method from child class c#
提问by CR41G14
This is a slightly different question from previous answers I have seen or I am not getting it. I have a parent class with a method named MyMethod()and a variable public Int32 CurrentRow;
这是一个与我之前看到的答案略有不同的问题,或者我没有得到它。我有一个父类,其中包含一个名为的方法MyMethod()和一个变量public Int32 CurrentRow;
public void MyMethod()
{
this.UpdateProgressBar();
}
In the parent I create a new instance of ChildClass
在父级中,我创建了一个新实例 ChildClass
Boolean loadData = true;
if (loadData)
{
ChildClass childClass = new ChildClass();
childClass.LoadData(this.Datatable);
}
In the child Class LoadData()method I want to be able to set the CurrentRowvariable of the parent and call the MyMethod()function.
在子类LoadData()方法中,我希望能够设置CurrentRow父类的变量并调用MyMethod()函数。
How do I do this?
我该怎么做呢?
采纳答案by CR41G14
Found the solution.
找到了解决办法。
In the parent I declare a new instance of the ChildClass() then bind the event handler in that class to the local method in the parent
在父级中,我声明了一个 ChildClass() 的新实例,然后将该类中的事件处理程序绑定到父级中的本地方法
In the child class I add a public event handler:
在子类中,我添加了一个公共事件处理程序:
public EventHandler UpdateProgress;
In the parent I create a new instance of this child class then bind the local parent event to the publiceventhandlerin the child
在父类中,我创建了这个子类的新实例,然后将本地父事件绑定到子类publiceventhandler中
ChildClass child = new ChildClass();
child.UpdateProgress += this.MyMethod;
child.LoadData(this.MyDataTable);
Then in the LoadData()of the child class I can call
然后在LoadData()子类的我可以打电话
private LoadData() {
this.OnMyMethod();
}
Where OnMyMethodis:
在哪里OnMyMethod:
public void OnMyMethod()
{
// has the event handler been assigned?
if (this.UpdateProgress!= null)
{
// raise the event
this.UpdateProgress(this, new EventArgs());
}
}
This runs the event in the parent class
这在父类中运行事件
回答by Rory McCrossan
To access properties and methods of a parent class use the basekeyword. So in your child class LoadData()method you would do this:
要访问父类的属性和方法,请使用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...
}
}
Note that you would also have to change the access modifier of your parent MyMethod()to at least protectedfor the child class to access it.
请注意,您还必须将父类的访问修饰符更改MyMethod()为至少protected子类才能访问它。
回答by Jamiec
One way to do this would be to pass the instance of ParentClassto the ChildClasson construction
一种方法是将 的实例传递ParentClass给ChildClasson 构造
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
}
}
Make sure to pass the reference to thiswhen constructing ChildClassinside parent:
在父内部this构造时,请确保将引用传递给ChildClass:
if(loadData){
ChildClass childClass = new ChildClass(this); // here
childClass.LoadData(this.Datatable);
}
Caveat: This is probably notthe best way to organise your classes, but it directly answers your question.
警告:这可能不是组织课程的最佳方式,但它直接回答了您的问题。
EDIT: In the comments you mention that more than 1 parent class wants to use ChildClass. This is possible with the introduction of an interface, eg:
编辑:在您提到的评论中,超过 1 个父类想要使用ChildClass. 通过引入接口,这是可能的,例如:
public interface IParentClass
{
void UpdateProgressBar();
int CurrentRow{get; set;}
}
Now, make sure to implement that interface on both (all?) Parent Classes and change child class to this:
现在,确保在两个(所有?)父类上都实现该接口并将子类更改为:
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
}
}
Now anything that implements IParentClasscan construct an instance of ChildClassand pass thisto its constructor.
现在任何实现IParentClass都可以构造一个实例ChildClass并传递this给它的构造函数。
回答by ND77
To follow up on the comment by suhendri to Rory McCrossan answer. Here is an Action delegate example:
跟进 suhendri 对 Rory McCrossan 回答的评论。这是一个 Action 委托示例:
In child add:
在子项中添加:
public Action UpdateProgress; // In place of event handler declaration
// declare an Action delegate
.
.
.
private LoadData() {
this.UpdateProgress(); // call to Action delegate - MyMethod in
// parent
}
In parent add:
在父级中添加:
// The 3 lines in the parent becomes:
ChildClass child = new ChildClass();
child.UpdateProgress = this.MyMethod; // assigns MyMethod to child delegate

