脚本宝典收集整理的这篇文章主要介绍了Java知识点总结(注解-内置注解),脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
Java知识点总结(注解-内置注解)
@(Java知识点总结)[Java, 注解]
@override
- 定义在java.lang.Override 中,此注释只适用于修饰方法,表示一个方法声明打算重写父类的另一个方法声明。
public class Demo01 { @Override public String toString() { return ""; } }
源码
import java.lang.annotation.*; /** * Indicates that a method declaration is intended to override a * method declaration in a supertype. If a method is annotated with * this annotation type compilers are required to generate an error * message unless at least one of the following conditions hold: * * <ul><li> * The method does override or implement a method declared in a * supertype. * </li><li> * The method has a signature that is override-equivalent to that of * any public method declared in {@linkplain Object}. * </li></ul> * * @author Peter von der Ahé * @author Joshua Bloch * @jls 9.6.1.4 @Override * @since 1.5 */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.SOURCE) public @interface Override { }
@DePRecated
- 定义在java.lang.Deprecated中,遗弃、废弃,不建议使用。此注释可用于修饰方法、属性、类,表示不鼓励程序员使用这样的元素,通常是因为它很危险或存在更好的选择。
public class Demo01 { @Deprecated public static void test1(){ } public static void main(String[] args) { test1(); } }
源码
import java.lang.annotation.*; import static java.lang.annotation.ElementType.*; /** * A program element annotated @Deprecated is one that programmers * are discouraged From using, typically because it is dangerous, * or because a better alternative exists. Compilers warn when a * deprecated program element is used or overridden in non-deprecated code. * * @author Neal Gafter * @since 1.5 * @jls 9.6.3.6 @Deprecated */ @Documented @Retention(RetentionPolicy.RUNTIME) @Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE}) public @interface Deprecated { }
@SupPressWarings
- 定义在java.lang.SuppressWarnings中,用来抑制编译时的警告信息。
- 与前两个注释有所不同, 你需要添加一个参数才能正确使用 ,这些参数值是已经定义好了的,我们选择性的使用就好了,参数如下:
参数 说明 deprecation 使用了过时的类或方法的警告 unchecked 执行了未检查的转换时的警告,如使用集合时未指定泛型 fallthrough 当在switch语句使用时发生case穿透 path 在类路径、源文件路径等中有不存在路径的警告 serial 当在可序列化的类上缺少serialVersionUID定义时的警告 finally 任何finally子句不能完成时的警告 all 关于以上所有情况的警告
@SuppressWarnings("unchecked") @SuppressWarnings(value={"unchecked","deprecation"})
import java.util.ArrayList; import java.util.List; public class Demo01 { @SuppressWarnings("all") public static void test2(){ List list = new ArrayList(); }
源码
package java.lang; import java.lang.annotation.*; import static java.lang.annotation.ElementType.*; /** * Indicates that the named compiler warnings should be suppressed in the * annotated element (and in all program elements contained in the annotated * element). Note that the set of warnings suppressed in a given element is * a superset of the warnings suppressed in all containing elements. For * example, if you annotate a class to suppress one warning and annotate a * method to suppress another, both warnings will be suppressed in the method. * * <p>As a matter of style, programmers should always use this annotation * on the most deeply nested element where it is effective. If you want to * suppress a warning in a particular method, you should annotate that * method rather than its class. * * @author Josh Bloch * @since 1.5 * @jls 4.8 Raw Types * @jls 4.12.2 Variables of Reference Type * @jls 5.1.9 Unchecked Conversion * @jls 5.5.2 Checked Casts and Unchecked Casts * @jls 9.6.3.5 @SuppressWarnings */ @Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE}) @Retention(RetentionPolicy.SOURCE) public @interface SuppressWarnings { /** * The set of warnings that are to be suppressed by the compiler in the * annotated element. Duplicate names are permitted. The second and * successive occurrences of a name are ignored. The presence of * unrecognized warning names is <i>not</i> an error: Compilers must * ignore any warning names they do not recognize. They are, however, * free to emit a warning if an annotation contains an unrecognized * warning name. * * <p> The string {@code "unchecked"} is used to suppress * unchecked warnings. Compiler vendors should document the * additional warning names they support in conjunction with this * annotation type. They are encouraged to cooperate to ensure * that the same names work across multiple compilers. * @return the set of warnings to be suppressed */ String[] value(); }
以上是脚本宝典为你收集整理的Java知识点总结(注解-内置注解)全部内容,希望文章能够帮你解决Java知识点总结(注解-内置注解)所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。