C#继承方法,带有一些继承逻辑和一些自定义逻辑-这可能吗?
我有一份申请书;有一个包含许多子类的父类。我希望父类中有一个方法,其中包含一些逻辑,并让每个子类向其添加自定义逻辑,以便当任何子类调用该方法时,它首先运行父类中定义的一些代码,然后运行子类中定义的自定义部分。这能做到吗?如果没有,实现这种代码执行的最佳方法是什么?是的,可以通过在基类中定义一个虚拟方法,并在需要“插入”自定义逻辑的位置从“有效负载”方法调用它来实现。将此方法抽象化是很常见的:C#继承方法,带有一些继承逻辑和一些自定义逻辑-这可能吗?,c#,inheritance,C#,Inheritance,我有一份申请书;有一个包含许多子类的父类。我希望父类中有一个方法,其中包含一些逻辑,并让每个子类向其添加自定义逻辑,以便当任何子类调用该方法时,它首先运行父类中定义的一些代码,然后运行子类中定义的自定义部分。这能做到吗?如果没有,实现这种代码执行的最佳方法是什么?是的,可以通过在基类中定义一个虚拟方法,并在需要“插入”自定义逻辑的位置从“有效负载”方法调用它来实现。将此方法抽象化是很常见的: abstract class MyBase { protected abstract void
abstract class MyBase {
protected abstract void CustomLogic(); // Subclasses implement this
public void PayloadMethod() {
... // Do somethig
CustomLogic();
... // Do something else
}
}
class Derived1 : MyBase {
protected override void CustomLogic() {
... // Custom logic 1
}
}
class Derived2 : MyBase {
protected override void CustomLogic() {
... // Custom logic 2
}
}
class Derived3 : MyBase {
protected override void CustomLogic() {
... // Custom logic 3
}
}
类层次结构的客户端实例化一个DerivedN
类,并调用PayloadMethod()
,作为调用的一部分,它调用CustomLogic
这种方法被调用。是的,可以通过在基类中定义一个虚拟方法,并在需要“插入”自定义逻辑的位置从“有效负载”方法调用它来实现。将此方法抽象化是很常见的:
abstract class MyBase {
protected abstract void CustomLogic(); // Subclasses implement this
public void PayloadMethod() {
... // Do somethig
CustomLogic();
... // Do something else
}
}
class Derived1 : MyBase {
protected override void CustomLogic() {
... // Custom logic 1
}
}
class Derived2 : MyBase {
protected override void CustomLogic() {
... // Custom logic 2
}
}
class Derived3 : MyBase {
protected override void CustomLogic() {
... // Custom logic 3
}
}
类层次结构的客户端实例化一个DerivedN
类,并调用PayloadMethod()
,作为调用的一部分,它调用CustomLogic
这种方法被称为。实现它的一种方法是定义一个非虚拟方法作为入口点,执行基类中定义的代码,然后调用一个虚拟(或抽象)受保护的方法,子类可以(或必须)重写该方法,如下所示:
abstract class Foo
{
public void Bar()
{
// some code defined in the parent class
BarCore(); // the customized part of it as defined in the child class
}
protected virtual void BarCore() { }
}
实现它的一种方法是将非虚拟方法定义为入口点,该入口点执行基类中定义的代码,然后调用子类可以(或必须)重写的虚拟(或抽象)受保护方法,如下所示:
abstract class Foo
{
public void Bar()
{
// some code defined in the parent class
BarCore(); // the customized part of it as defined in the child class
}
protected virtual void BarCore() { }
}
实现这一点的最简单方法是采用两种方法:
class BaseClass
{
public void DoSomething()
{
// base class code
// derived class code, modifiable by the derived class
this.DoItSpecificallyForThatDerivedClass();
}
protected abstract void DoItSpecificallyForThatDerivedClass();
}
public class ADerivedClass : BaseClass
{
protected override void DoItSpecificallyForThatDerivedClass()
{
// code specific to this instance and/or class
}
}
实现这一点的最简单方法是采用两种方法:
class BaseClass
{
public void DoSomething()
{
// base class code
// derived class code, modifiable by the derived class
this.DoItSpecificallyForThatDerivedClass();
}
protected abstract void DoItSpecificallyForThatDerivedClass();
}
public class ADerivedClass : BaseClass
{
protected override void DoItSpecificallyForThatDerivedClass()
{
// code specific to this instance and/or class
}
}
你可以有一个委托,一个抽象方法,等等。你也可以在子类中从
method()
调用base.method()
。@DanielA.White你能解释一下你的意思吗?@itsme86这是一个比我到目前为止所知道的更好的主意。不过,它确实需要在每个子类的方法开头重复该行代码。您可以有一个委托、一个抽象方法等。您还可以从method()调用base.method()
在儿童课程中。@DanielA.White你能解释一下你的意思吗?@itsme86这是一个比我目前所知道的更好的主意。不过,它确实需要在每个子类的方法开头重复该行代码。