[英]Call again abstract inherited method from another class
I am using abstract factory pattern. 我正在使用抽象工厂模式。 I need to call an abstract method which is called the
HelperClass
. 我需要调用一个称为
HelperClass
的抽象方法。
For example: I have two abstract derived classes. 例如:我有两个抽象派生类。 I create
ClassA
, then ClassA
calls TakeAction
method. 我创建
ClassA
,然后ClassA
调用TakeAction
方法。 TakeAction
method needs to create another method which uses HelperClass
and call class the method
SolveProblem`. TakeAction
方法需要创建一个使用另一种方法HelperClass
和调用class the method
SolveProblem`。
Here is my question: How can I call again a ClassA
method in the HelperClass
? 这里是我的问题:我怎么能再次调用
ClassA
方法,在HelperClass
? Because SolveProblem have some logic and it will be decide to return or not. 因为SolveProblem具有某些逻辑,所以将决定是否返回。
class Program {
static void Main(string[] args) {
var classA = new ClassA();
classA.StartOperation();
}
}
public abstract class ClassAbstract {
public abstract void StartOperation();
public void TakeAction() {
var helper = new HelperClass();
helper.SolveProblem(DayOfWeek.Sunday);
}
public abstract void WeekDayOperation();
public abstract void WeekEndOperation();
}
public class ClassA : ClassAbstract {
public override void StartOperation() {
TakeAction();
}
public override void WeekDayOperation() {
}
public override void WeekEndOperation() {
throw new NotImplementedException();
}
}
public class ClassB : ClassAbstract {
public override void StartOperation() {
TakeAction();
}
public override void WeekDayOperation() {
}
public override void WeekEndOperation() {
throw new NotImplementedException();
}
}
public class HelperClass {
public void SolveProblem(DayOfWeek day) {
//Problem solved. I need to call "CallThisMethod" who called this helper class
//How to I Call
switch(day) {
case DayOfWeek.Sunday:
case DayOfWeek.Saturday:
ClassA.WeekEndOperation();
break;
case DayOfWeek.Friday:
FridayOperations();
break;
default:
ClassA.WeekDayOperation();
break;
}
}
public void FridayOperations() {
}
}
One solution would be to pass an instance of type: ClassAbstract
as parameter into the method SolveProblem
. 一种解决方案是将类型实例:
ClassAbstract
作为参数传递给方法SolveProblem
。 You can use this variable then to call the method. 您可以使用此变量然后调用该方法。
public void SolveProblem(DayOfWeek day, ClassAbstract c)
{
//Problem solved. I need to call "CallThisMethod" who called this helper class
//How to I Call
switch (day)
{
case DayOfWeek.Sunday:
case DayOfWeek.Saturday:
c.WeekEndOperation();
break;
case DayOfWeek.Friday:
FridayOperations();
break;
default:
c.WeekDayOperation();
break;
}
}
To close the circle you need to pass the current instnace ( this
) when calling this method in the base implementation of TakeAction
in the ClassAbstract
: 要关闭你需要传递当前instnace圆(
this
)调用基实现的此方法时TakeAction
在ClassAbstract
:
public abstract class ClassAbstract
{
public abstract void StartOperation();
public void TakeAction()
{
var helper = new HelperClass();
helper.SolveProblem(DayOfWeek.Sunday, this);
}
By the phenomenon of polymorphism the correct implementation of the override will be called. 通过多态现象,将调用覆盖的正确实现。 In you example from above it will be the overrides of the methods
WeekEndOperation
and WeekDayOperation
of the class ClassA
在上面的示例中,将替代类
ClassA
WeekEndOperation
和WeekDayOperation
方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.