原来 java 注解只是个标记,没什么本领,一文精通,值得保藏!
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">前言</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">java里有个神奇的存在,注解,<span style="color: black;">便是</span>那个天天@别人的家伙,它到底是何方神圣啊?</p> 本文由 二当家的白帽子 <a style="color: black;">二当家的白帽子_CSDN博客-算法,LintCode领扣算法题目,Java语言<span style="color: black;">行业</span>博主</a> 博客原创,转载请注明<span style="color: black;">源自</span>,谢谢~<h2 style="color: black; text-align: left; margin-bottom: 10px;">什么是注解</h2>从JDK5<span style="color: black;">起始</span>,Java<span style="color: black;">增多</span>对元数据的支持,<span style="color: black;">亦</span><span style="color: black;">便是</span>注解,注解与注释是有<span style="color: black;">必定</span>区别的,<span style="color: black;">能够</span>把注解理解为代码里的特殊标记,这些标记<span style="color: black;">能够</span>在编译,类加载,运行时被读取,并执行相应的处理。<span style="color: black;">经过</span>注解<span style="color: black;">研发</span>人员<span style="color: black;">能够</span>在不改变原有代码和<span style="color: black;">规律</span>的<span style="color: black;">状况</span>下在源代码中嵌入<span style="color: black;">弥补</span>信息。<h2 style="color: black; text-align: left; margin-bottom: 10px;">内置的注解</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">二当家的<span style="color: black;">发掘</span>在 IDE 中<span style="color: black;">倘若</span>创建一个类并实现一个接口之后,<span style="color: black;">哪些</span>实现接口的<span style="color: black;">办法</span>上面会自动帮我添加 @Override 的标记。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">而这个标记<span style="color: black;">便是</span>注解,像@Override<span style="color: black;">这般</span>JDK内置的注解还有好几个呢,<span style="color: black;">她们</span>都在java.lang包下面,<span style="color: black;">咱们</span>分别<span style="color: black;">瞧瞧</span>。</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;">@Override</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">当子类重写父类<span style="color: black;">办法</span>时,子类<span style="color: black;">能够</span>加上这个注解,那这有什么什么用?这<span style="color: black;">能够</span><span style="color: black;">保证</span>子类确实重写了父类的<span style="color: black;">办法</span>,避免<span style="color: black;">显现</span>低级错误。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://pic1.zhimg.com/80/v2-7baf5fc4f10bf9012cd528541b9040a8_720w.webp" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">起始</span>不<span style="color: black;">晓得</span>有什么用,直到有一天,我把接口里的这个<span style="color: black;">办法</span>删除了,结果就编译不<span style="color: black;">经过</span>了。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://pic4.zhimg.com/80/v2-63c50e1591a3252af7469171b3a7c94f_720w.webp" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">下面是直接在命令行编译的结果。这回我<span style="color: black;">晓得</span>这个标记的<span style="color: black;">功效</span>了,它<span style="color: black;">能够</span>告诉编译器和阅读源码的人这个<span style="color: black;">办法</span>是覆盖或实现超类型的<span style="color: black;">办法</span>。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://pic1.zhimg.com/80/v2-f35ebbcaf542d4945e7dd3a9a3739ca0_720w.webp" style="width: 50%; margin-bottom: 20px;"></div>
<h2 style="color: black; text-align: left; margin-bottom: 10px;">@Deprecated</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这个注解用于<span style="color: black;">暗示</span>某个程序元素类,<span style="color: black;">办法</span>等已过时,当其他程序<span style="color: black;">运用</span>已过时的类,<span style="color: black;">办法</span>时编译器会给出警告(删除线,这个见了不少了吧)。</p>
感谢您的精彩评论,为我带来了新的思考角度。 我完全同意你的观点,说得太对了。
页:
[1]