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

查看:161
本文介绍了@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.

推荐答案

我不太明白为什么你不希望使用abstract修饰符 - 这是供子类强制实施,只需要用于一些方法中,不是全部。或者,也许你正在考虑的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天全站免登陆