再次调用从另一个 class 抽象继承的方法
Call again abstract inherited method from another class
我正在使用抽象工厂模式。我需要调用一个名为 HelperClass
的抽象方法。
例如:我有两个摘要导出类。我创建 ClassA
,然后 ClassA
调用 TakeAction
方法。 TakeAction
方法需要创建另一个使用 HelperClass
并调用 class the method
SolveProblem` 的方法。
这是我的问题:如何在 HelperClass
中再次调用 ClassA
方法?因为 SolveProblem 有一些逻辑,它将决定是否 return。
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() {
}
}
一个解决方案是将类型的实例:ClassAbstract
作为参数传递到方法 SolveProblem
中。您可以使用此变量然后调用该方法。
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;
}
}
在ClassAbstract
:
中TakeAction
的基本实现中调用此方法时,要关闭圆圈需要传递当前实例(this
)
public abstract class ClassAbstract
{
public abstract void StartOperation();
public void TakeAction()
{
var helper = new HelperClass();
helper.SolveProblem(DayOfWeek.Sunday, this);
}
由现象polymorphism 覆盖的正确执行将被调用。在上面的示例中,它将覆盖 class ClassA
的方法 WeekEndOperation
和 WeekDayOperation
我正在使用抽象工厂模式。我需要调用一个名为 HelperClass
的抽象方法。
例如:我有两个摘要导出类。我创建 ClassA
,然后 ClassA
调用 TakeAction
方法。 TakeAction
方法需要创建另一个使用 HelperClass
并调用 class the method
SolveProblem` 的方法。
这是我的问题:如何在 HelperClass
中再次调用 ClassA
方法?因为 SolveProblem 有一些逻辑,它将决定是否 return。
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() {
}
}
一个解决方案是将类型的实例:ClassAbstract
作为参数传递到方法 SolveProblem
中。您可以使用此变量然后调用该方法。
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;
}
}
在ClassAbstract
:
TakeAction
的基本实现中调用此方法时,要关闭圆圈需要传递当前实例(this
)
public abstract class ClassAbstract
{
public abstract void StartOperation();
public void TakeAction()
{
var helper = new HelperClass();
helper.SolveProblem(DayOfWeek.Sunday, this);
}
由现象polymorphism 覆盖的正确执行将被调用。在上面的示例中,它将覆盖 class ClassA
WeekEndOperation
和 WeekDayOperation