加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

JAVA8接口中的default、static方法使用注意事项

发布时间:2020-12-14 06:38:47 所属栏目:Java 来源:网络整理
导读:转自: p style="color:rgb(63,63,63);font-family:'microsoft yahei';font-size:15px;" 普通人大部分甚至可能全部都是告诫子女读书毕业找份好工作; 后者更多要求考TOEFL、GRE、出国留学回来做高管、出资给孩子创业; default方法 java.lang.Iterable 接口

转自:

<p style="color:rgb(63,63,63);font-family:'microsoft yahei';font-size:15px;">

    普通人大部分甚至可能全部都是告诫子女读书毕业找份好工作;

  • 后者更多要求考TOEFL、GRE、出国留学回来做高管、出资给孩子创业;

default方法

java.lang.Iterable接口中有一个默认的方法实现:

  forEach(Consumer T> action) {
        Objects.requireNonNull(action);
         (T t : ) {
            action.accept(t);
        }
    }
    1
  • 2
  • 3
  • 4
  • 5
  • 6

default关键字修饰,它是对象方法,需要使用对象来进行访问。

@FunctionalInterface?表明该接口是一个函数式接口,只能拥有 一个抽象方法。

 com.byron4j.hightLevel.java8.lambda;

<span class="hljs-javadoc" style="color:rgb(136,0);">/**

  • 接口类型 拥有自己的default、static方法实现
  • <span class="hljs-javadoctag" style="color:rgb(102,102);"> @FunctionalInterface 表明该接口是一个函数式接口,只能拥有 一个抽象方法
* Byron.Y.Y */

<span class="hljs-annotation" style="color:rgb(155,133,157);">@FunctionalInterface
<span class="hljs-keyword" style="color:rgb(0,136);">public <span class="hljs-class"><span class="hljs-keyword" style="color:rgb(0,136);">interface <span class="hljs-title" style="color:rgb(102,102);">DefaultStaticMethodDemo {

<span class="hljs-comment" style="color:rgb(136,0);"&gt;/*非default、static方法不能有实现
 * --否则编译错误--Abstract methods do not specify a body
void sayHello4CompilerError(){};
*/</span>

<span class="hljs-keyword" style="color:rgb(0,136);"&gt;void</span> sayHello();



<span class="hljs-comment" style="color:rgb(136,0);"&gt;/*default、static方法必须有具体的实现
 * --否则编译错误--This method requires a body instead of a semicolon
default void studyTarget();
*/</span>

<span class="hljs-keyword" style="color:rgb(0,136);"&gt;void</span> studyTarget(){
    System.out.println(<span class="hljs-string" style="color:rgb(0,0);"&gt;"出生"</span>);
    System.out.println(<span class="hljs-string" style="color:rgb(0,0);"&gt;"t--> 注入知识"</span>);
    System.out.println(<span class="hljs-string" style="color:rgb(0,0);"&gt;"tt--> 生命消亡"</span>);
}

<span class="hljs-comment" style="color:rgb(136,0);"&gt;//可以拥有多个default方法</span>
<span class="hljs-keyword" style="color:rgb(0,136);"&gt;void</span> studyTarget2(){
    System.out.println(<span class="hljs-string" style="color:rgb(0,0);"&gt;"DefaultStaticMethodDemo#【default】studyTarget2 invok."</span>);
}

<span class="hljs-comment" style="color:rgb(136,0);"&gt;//可以拥有多个static方法</span>
<span class="hljs-keyword" style="color:rgb(0,136);"&gt;static</span> <span class="hljs-keyword" style="color:rgb(0,136);"&gt;void</span> info(){
    System.out.println(<span class="hljs-string" style="color:rgb(0,0);"&gt;"DefaultStaticMethodDemo#【static】 info invok."</span>);
}



<span class="hljs-keyword" style="color:rgb(0,136);"&gt;public</span> <span class="hljs-keyword" style="color:rgb(0,136);"&gt;void</span> <span class="hljs-title"&gt;main</span>(String[] args) {
    info();

    <span class="hljs-keyword" style="color:rgb(0,136);"&gt;new</span> DefaultStaticMethodDemo() {
        <span class="hljs-comment" style="color:rgb(136,0);"&gt;//仅仅需要实现抽象方法</span>
        <span class="hljs-comment" style="color:rgb(136,0);"&gt;//default、static方法不需要强制自己新实现</span>
        <span class="hljs-annotation" style="color:rgb(155,157);"&gt;@Override</span>
        <span class="hljs-keyword" style="color:rgb(0,136);"&gt;void</span> <span class="hljs-title"&gt;sayHello</span>() {
            <span class="hljs-comment" style="color:rgb(136,0);"&gt;// TODO Auto-generated method stub</span>

        }
    };
}

}

<ul class="pre-numbering" style="width:50px;border-right-width:1px;border-right-style:solid;border-right-color:rgb(221,221);list-style:none;text-align:right;">

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 接口中方法使用的注意事项

      非default、static方法不能有实现,否则编译错误:Abstract methods do not specify a body

    • default、static方法必须有具体的实现,否则编译错误:This method requires a body instead of a semicolon

    • 可以拥有多个default方法

    • 可以拥有多个static方法

    • 使用接口中类型时,仅仅需要实现抽象方法,default、static方法不需要强制自己新实现

    实现多个接口引发的问题

    多个接口存在签名一样的default方法导致编译错误

     Byron.Y.Y
     */
    

    <span class="hljs-keyword" style="color:rgb(0,102);">DefaultStaticMethodDemo2 {

    <span class="hljs-keyword" style="color:rgb(0,0);"&gt;"tt--> 生命消亡"</span>);
    }

    }


    <ul class="pre-numbering" style="width:50px;border-right-width:1px;border-right-style:solid;border-right-color:rgb(221,221);list-style:none;text-align:right;">

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  •  com.byron4j.hightLevel.java8.lambda;
    

    <span class="hljs-comment" style="color:rgb(136,0);">//编译错误:Duplicate default methods named studyTarget
    <span class="hljs-comment" style="color:rgb(136,0);">// with the parameters () and () are inherited from the
    <span class="hljs-comment" style="color:rgb(136,0);">//types DefaultStaticMethodDemo2 and DefaultStaticMethodDemo
    <span class="hljs-keyword" style="color:rgb(0,136);">class <span class="hljs-title" style="color:rgb(102,102);">SubClassDemo <span class="hljs-keyword" style="color:rgb(0,136);">implements
    <span class="hljs-title" style="color:rgb(102,102);">DefaultStaticMethodDemo,<span class="hljs-title" style="color:rgb(102,102);">DefaultStaticMethodDemo2{

    }


    <ul class="pre-numbering" style="width:50px;border-right-width:1px;border-right-style:solid;border-right-color:rgb(221,221);list-style:none;text-align:right;">

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 继承抽象类同时实现接口引发的问题

    如果一个类实现了某个拥有default方法的接口的话,在该类中则不需要自己再次实现该default方法了。

    但是如果该类实现接口时,还继承了某个抽象类,该抽象类拥有一个和default签名一样的抽象方法,则在该类中必须重写抽象方法(也是接口中的该default方法):

    抽象类AbstractClassDemo 拥有和接口DefaultStaticMethodDemo同安用的签名方法studyTarget:

    <span class="hljs-keyword" style="color:rgb(0,136);">abstract <span class="hljs-keyword" style="color:rgb(0,136);">class AbstractClassDemo {
    <span class="hljs-keyword" style="color:rgb(0,136);">void studyTarget();
    }

    <ul class="pre-numbering" style="width:50px;border-right-width:1px;border-right-style:solid;border-right-color:rgb(221,63);font-family:'microsoft yahei';font-size:15px;">
    类SubClassDemo2 必须重写studyTarget方法


    <pre class="prettyprint" style="font-family:'Source Code Pro',0);">/**

    • 继承抽象类

    • 实现接口

    • 抽象类、接口存在同样的签名方法

    • 抽象类未有实现体;接口中default实现了方法。

    • * Byron.Y.Y */ {

      <span class="hljs-annotation" style="color:rgb(155,157);">@Override
      <span class="hljs-keyword" style="color:rgb(0,136);">void <span class="hljs-title">sayHello() {
      <span class="hljs-comment" style="color:rgb(136,0);">// TODO Auto-generated method stub

      }

      <span class="hljs-annotation" style="color:rgb(155,136);">void <span class="hljs-title">studyTarget() {
      <span class="hljs-comment" style="color:rgb(136,0);">// TODO Auto-generated method stub

      }

    }

    <ul class="pre-numbering" style="width:50px;border-right-width:1px;border-right-style:solid;border-right-color:rgb(221,221);list-style:none;text-align:right;">

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 其他注意事项

    抽象类、接口存在同样的签名方法,抽象类有实现体但是不是public修饰的;—-> 编译错误:抽象接口中的实现不能隐藏接口中的方法;—->解决办法:将抽象类中的方法访问控制符使用public修饰。

     AbstractClassDemo2 {
         sayHello(){
            System..println();
        }
    }
    
      1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
     com.byron4j.hightLevel.java8.lambda;
    

    <span class="hljs-javadoc" style="color:rgb(136,0);">/**

    • 继承抽象类
    • 实现接口
    • 抽象类、接口存在同样的签名方法
    • 抽象类有实现体但是不是public修饰的;
      -------------编译错误:抽象接口中的实现不能隐藏接口中的方法
      -------------解决办法:将抽象类中的方法访问控制符使用public修饰
    * {

    }

    <ul class="pre-numbering" style="width:50px;border-right-width:1px;border-right-style:solid;border-right-color:rgb(221,63);font-family:'microsoft yahei';font-size:15px;">
    抽象类AbstractClassDemo2 拥有和接口DefaultStaticMethodDemo相同签名的方法sayHello,但是AbstractClassDemo2 的实现不是public的引发编译错误。

    (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

      推荐文章
        热点阅读