C# 忽略基类方法内的方法调用
重写基类方法时,如何忽略基类方法内部的方法调用?与下面的示例类似,执行所有100行代码,忽略在派生类中调用方法C# 忽略基类方法内的方法调用,c#,oop,C#,Oop,重写基类方法时,如何忽略基类方法内部的方法调用?与下面的示例类似,执行所有100行代码,忽略在派生类中调用方法insideMethod1()。有没有不重复代码的方法 public class A virtual method1(){ // 100 lines of code insideMethod1(); } public class B : A override method1(){ // call the 100 lines of code but ignore calling ins
insideMethod1()
。有没有不重复代码的方法
public class A
virtual method1(){
// 100 lines of code
insideMethod1();
}
public class B : A
override method1(){
// call the 100 lines of code but ignore calling insideMethod1()
}
您可以反其道而行之-覆盖
insideMethod1
,使其不会执行任何操作:
public class A
{
public void method1() {
// 100 lines of code
insideMethod1();
}
protected virtual void insideMethod1() { /* some work here */ }
}
public class B : A
{
protected override void insideMethod1() { }
}
进一步阅读:您可以反其道而行之-覆盖
insideMethod1
,使其不会执行任何操作:
public class A
{
public void method1() {
// 100 lines of code
insideMethod1();
}
protected virtual void insideMethod1() { /* some work here */ }
}
public class B : A
{
protected override void insideMethod1() { }
}
进一步阅读:我对c#不太熟悉,但我认为可以将bool参数传递给函数,该函数决定是否调用嵌套函数
public class A
virtual method1(bool callInsideMethod = true){
// 100 lines of code
if(callInsideMethod)
insideMethod1();
}
public class B : A
override method1(bool callInsideMethod = false){
// call the 100 lines of code but ignore calling insideMethod1()
}
我不太熟悉c#,但我认为可以将bool参数传递给函数,该函数决定是否调用嵌套函数
public class A
virtual method1(bool callInsideMethod = true){
// 100 lines of code
if(callInsideMethod)
insideMethod1();
}
public class B : A
override method1(bool callInsideMethod = false){
// call the 100 lines of code but ignore calling insideMethod1()
}
您应该重构代码,这样您就不需要这样做。重写方法1中的100行代码与基类中的相同吗?您应该重构代码,这样您就不需要这样做。重写方法1中的100行代码与基类中的相同吗?