@MustOverride 注释? [英] @MustOverride annotation?

查看:20
本文介绍了@MustOverride 注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 .NET 中,可以为特定超类中的方法指定mustoverride"属性,以确保子类覆盖该特定方法.我想知道是否有人有可以达到相同效果的自定义 java 注释.基本上我想要的是推动子类覆盖超类中的方法,该方法本身具有一些必须贯穿的逻辑.我不想使用抽象方法或接口,因为我希望在超级方法中运行一些通用功能,但或多或​​少会产生编译器警告/错误,表示派生类应该覆盖给定的方法.

In .NET, one can specify a "mustoverride" attribute to a method in a particular superclass to ensure that subclasses override that particular method. I was wondering whether anybody has a custom java annotation that could achieve the same effect. Essentially what i want is to push for subclasses to override a method in a superclass that itself has some logic that must be run-through. I dont want to use abstract methods or interfaces, because i want some common functionality to be run in the super method, but more-or-less produce a compiler warning/error denoting that derivative classes should override a given method.

推荐答案

我不太明白你为什么不想使用抽象修饰符——这是为了强制子类实现,只需要用于某些方法,而不是全部.或者,您可能正在考虑 C++ 风格的纯抽象"类?

I don't quite see why you would not want to use abstract modifier -- this is intended for forcing implementation by sub-class, and only need to be used for some methods, not all. Or maybe you are thinking of C++ style "pure abstract" classes?

但许多 Java 开发人员不知道的另一件事是,也可以覆盖非抽象方法并将它们声明为抽象;喜欢:

But one other thing that many Java developers are not aware of is that it is also possible to override non-abstract methods and declare them abstract; like:

public abstract String toString(); // force re-definition

这样即使 java.lang.Object 已经定义了一个实现,您也可以强制子类重新定义它.

so that even though java.lang.Object already defines an implementation, you can force sub-classes to define it again.

这篇关于@MustOverride 注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆