将抽象函数设置为抽象 C#
Set as abstract an abstract function C#
我可以将抽象函数设置为抽象吗?我可以这样做吗:
public abstract class A
{
protected abstract void WhateverFunction();
}
public abstract class B:A
{
protected abstract void WhateverFunction();
}
public abstract class C:B
{
protected override void WhateverFunction()
{
//code here
}
}
如果没有,我可以做些什么来模拟这种行为?
可以,但是您需要在 B
class 上声明的函数中添加 override
修饰符。因此,在这种情况下,WhateverFunction
是抽象的,同时覆盖了 A
:
上的函数
public abstract class A
{
protected abstract void WhateverFunction();
}
public abstract class B : A
{
protected abstract override void WhateverFunction(); // HERE
}
public abstract class C : B
{
protected override void WhateverFunction()
{
//code here
}
}
在这种情况下,您也可以简单地省略 class B 上的 WhateverFunction
以获得相同的结果。
我可以将抽象函数设置为抽象吗?我可以这样做吗:
public abstract class A
{
protected abstract void WhateverFunction();
}
public abstract class B:A
{
protected abstract void WhateverFunction();
}
public abstract class C:B
{
protected override void WhateverFunction()
{
//code here
}
}
如果没有,我可以做些什么来模拟这种行为?
可以,但是您需要在 B
class 上声明的函数中添加 override
修饰符。因此,在这种情况下,WhateverFunction
是抽象的,同时覆盖了 A
:
public abstract class A
{
protected abstract void WhateverFunction();
}
public abstract class B : A
{
protected abstract override void WhateverFunction(); // HERE
}
public abstract class C : B
{
protected override void WhateverFunction()
{
//code here
}
}
在这种情况下,您也可以简单地省略 class B 上的 WhateverFunction
以获得相同的结果。