<div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article">JDK8新特性:函数式接口@FunctionalInterface的使用说明</h1>
</div>
<div class="article-info-box">
<div class="article-bar-top">
<!--文章类型-->
<span class="article-type type-1 float-left">原创</span> <a class="follow-nickName" href="https://me.csdn.net/aitangyong" target="_blank" rel="noopener">aitangyong</a>
<span class="time">最后发布于2017-01-06 14:03:29 </span>
<span class="read-count">阅读数 35076</span>
<a id="blog_detail_zk_collection" data-report-click="{"mod":"popu_823"}">
<svg class="icon">
<use xlink:href="#icon-csdnc-Collection-G"></use>
</svg>
收藏
</a>
</div>
<div class="up-time">发布于2017-01-06 14:03:29</div>
<div class="slide-content-box" style="display: block;">
<div class="tags-box artic-tag-box">
<span class="label">分类专栏:</span>
<a class="tag-link" target="_blank" rel="noopener" href="https://blog.csdn.net/aitangyong/category_1758853.html">
java </a>
</div>
<div class="article-copyright">
<span class="creativecommons">
<a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/"></a>
<span>
版权声明:本文为博主原创文章,遵循<a href="http://creativecommons.org/licenses/by-sa/4.0/" target="_blank" rel="noopener"> CC 4.0 BY-SA </a>版权协议,转载请附上原文出处链接和本声明。 </span>
<div class="article-source-link2222">
本文链接:<a href="https://blog.csdn.net/aitangyong/article/details/54137067">https://blog.csdn.net/aitangyong/article/details/54137067</a>
</div>
</span>
</div>
</div>
<div class="operating">
<a class="href-article-edit slide-toggle">收起</a>
</div>
</div>
</div>
</div>
<article class="baidu_pl">
<!--python安装手册开始-->
<!--python安装手册结束-->
<!--####专栏广告位图文切换开始-->
<!--####专栏广告位图文切换结束-->
<div id="article_content" class="article_content clearfix" style="height: 1875px; overflow: hidden;">
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-833878f763.css">
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-833878f763.css">
<div class="htmledit_views" id="content_views">
<p><span style="font-size:18px;">我们常用的一些接口Callable、Runnable、Comparator等在JDK8中都添加了<span style="font-size:18px;">@FunctionalInterface注解。</span></span></p>
<p><span style="font-size:18px;"><span style="font-size:18px;"><img src="https://www.icode9.com/i/?i=20170106133721767" alt=""><br></span></span></p>
<p><br></p>
<p><span style="font-size:18px;"><span style="font-size:18px;"></span></span></p>
<p><span style="font-size:18px;"><img src="https://www.icode9.com/i/?i=20170106135012602" alt=""><br></span></p>
<p><span style="font-size:18px;"></span></p>
<p><span style="font-size:18px;">通过JDK8源码javadoc,可以知道这个注解有以下特点:</span></p>
<p><span style="font-size:18px;">1、该注解只能标记在"有且仅有一个抽象方法"的接口上。</span></p>
<p><span style="font-size:18px;">2、JDK8接口中的<a href="http://blog.csdn.net/aitangyong/article/details/54134385">静态方法和默认方法</a>,都不算是抽象方法。</span></p>
<p><span style="font-size:18px;">3、接口默认继承java.lang.Object,所以如果接口显示声明覆盖了Object中方法,那么也不算抽象方法。</span></p>
<p><span style="font-size:18px;">4、该注解不是必须的,如果一个接口符合"函数式接口"定义,那么加不加该注解都没有影响。加上该注解能够更好地让编译器进行检查。如果编写的不是函数式接口,但是加上了@FunctionInterface,那么编译器会报错。</span></p>
<p><br></p>
<p><span style="font-size:18px;"><span style="font-size:18px;"><strong><span style="color:#ff0000;">@FunctionalInterface标记在接口上,“函数式接口”是指仅仅只包含一个抽象方法的接口</span></strong>。<br></span></span></p>
<p><span style="font-size:18px;"><span style="font-size:18px;"><img src="https://www.icode9.com/i/?i=20170106134212771" alt=""><br></span></span></p>
<p><span style="font-size:18px;"><span style="font-size:18px;"><br></span></span></p>
<p><span style="font-size:18px;"><span style="font-size:18px;">如果一个接口中包含不止一个抽象方法,那么不能使用@FunctionalInterface,编译会报错。</span></span></p>
<p><span style="font-size:18px;"><span style="font-size:18px;"><img src="https://www.icode9.com/i/?i=20170106134358818" alt=""><br></span></span></p>
<p><br></p>
<p><span style="font-size:18px;">比如下面这个接口就是一个正确的函数式接口:</span></p>
<pre><code class="language-java hljs"><ol class="hljs-ln"><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="1"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-comment">// 正确的函数式接口</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="2"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-meta">@FunctionalInterface</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="3"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">TestInterface</span> </span>{</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="4"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="5"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="6"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-comment">// 抽象方法</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="7"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">sub</span><span class="hljs-params">()</span></span>;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-comment">// java.lang.Object中的方法不是抽象方法</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">equals</span><span class="hljs-params">(Object var1)</span></span>;</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-comment">// default不是抽象方法</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">default</span> <span class="hljs-keyword">void</span> <span class="hljs-title">defaultMethod</span><span class="hljs-params">()</span></span>{</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="14"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="15"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> }</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="16"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="17"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-comment">// static不是抽象方法</span></div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="18"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">staticMethod</span><span class="hljs-params">()</span></span>{</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="19"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> </div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="20"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> }</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="21"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">}</div></div></li></ol></code><div class="hljs-button {2}" data-title="复制" onclick="hljs.copyCode(event)"></div></pre>
<p></p>
<p><br></p>
</div>
<div class="more-toolbox">
<div class="left-toolbox">
<ul class="toolbox-list">
<li class="tool-item tool-active is-like tool-clicked"><a href="javascript:;"><svg class="icon" aria-hidden="true">
<use xlink:href="#csdnc-thumbsup"></use>
</svg><span class="name">点赞</span>
<span class="count">11</span>
</a></li>
<li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{"mod":"popu_824"}"><svg class="icon" aria-hidden="true">
<use xlink:href="#icon-csdnc-Collection-G"></use>
</svg><span class="name">收藏</span></a></li>
<li class="tool-item tool-active is-share"><a href="javascript:;"><svg class="icon" aria-hidden="true">
<use xlink:href="#icon-csdnc-fenxiang"></use>
</svg>分享</a></li>
<!--打赏开始-->
<!--打赏结束-->
<li class="tool-item tool-more">
<a>
<svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
</a>
<ul class="more-box">
<li class="item"><a class="article-report">文章举报</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="person-messagebox">
<div class="left-message"><a href="https://blog.csdn.net/aitangyong">
<img src="https://profile.csdnimg.cn/1/B/3/3_aitangyong" class="avatar_pic" username="aitangyong">
<img src="https://g.csdnimg.cn/static/user-reg-year/1x/11.png" class="user-years">
</a></div>
<div class="middle-message">
<div class="title"><span class="tit"><a href="https://blog.csdn.net/aitangyong" data-report-click="{"mod":"popu_379"}" target="_blank">aitangyong</a></span>
</div>
<div class="text"><span>发布了335 篇原创文章</span> · <span>获赞 185</span> · <span>访问量 195万+</span></div>
</div>
<div class="right-message">
<a href="https://im.csdn.net/im/main.html?userName=aitangyong" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
</a>
<a class="btn btn-sm bt-button personal-watch" data-report-click="{"mod":"popu_379"}">关注</a>
</div>
</div>
</div>
</article>
</div>
莱克星顿的枪声 发布了57 篇原创文章 · 获赞 33 · 访问量 14万+ 私信 关注