Force base method call(强制基方法调用)
问题描述
Java 或 C# 中是否存在强制继承类调用基本实现的构造?您可以调用 super() 或 base() 但如果不调用它是否有可能引发编译时错误?那会很方便..
Is there a construct in Java or C# that forces inheriting classes to call the base implementation? You can call super() or base() but is it possible to have it throw a compile-time error if it isn't called? That would be very convenient..
--编辑--
我主要对重写方法感到好奇.
I am mainly curious about overriding methods.
推荐答案
没有也不应该这样做.
如果在基类中有这样的东西,我能想到的最接近的东西:
The closest thing I can think of off hand if something like having this in the base class:
public virtual void BeforeFoo(){}
public void Foo()
{
this.BeforeFoo();
//do some stuff
this.AfterFoo();
}
public virtual void AfterFoo(){}
并允许继承类覆盖 BeforeFoo 和/或 AfterFoo
And allow the inheriting class override BeforeFoo and/or AfterFoo
这篇关于强制基方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:强制基方法调用


- 如何指定 CORS 的响应标头? 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 获取数字的最后一位 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 转换 ldap 日期 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01