JDK1.8新特性

Posted zhaoyongqi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JDK1.8新特性相关的知识,希望对你有一定的参考价值。

jdk1.8新特性

 1 //JDK8之前
 2 interface IA{
 3     void doMethod01();
 4     void doMethod02();
 5 }
 6 //JDK8 中的默认方法,为什么要添加这样的方法呢?
 7 //便于直接在接口中添加新的方法,进行扩展.
 8 interface IB{
 9     //默认方法
10     default void doMethod01() {
11         System.out.println("doMethod01()");
12     }
13     //默认方法(特点,必须使用default修饰)
14     default void doMethod02() {
15         System.out.println("doMethod02()");
16     }
17     void doMethod03();
18     //JDK8以后接口中还允许有静态方法
19     static void doMethod04() {}
20 }
21 class B implements IB{
22     @Override
23     public void doMethod03() {
24         System.out.println("doMethod03()");
25     }
26 }
27 //JDK8中的 函数接口,使用@FunctionalInterface注解描述
28 @FunctionalInterface 
29 interface IC{ //此接口中只允许有一个抽象方法
30     void doMethod01();
31     default void doMethod02() {}
32     //JDK8以后接口中还允许有静态方法
33     static void doMethod03() {}
34 }
35 public class TestInterface01 {
36     static void doStudyInterface(IC c) {
37         c.doMethod01();
38     }
39     public static void main(String[] args) {
40         new IC() {
41             @Override
42             public void doMethod01() {
43                 System.out.println("helloworld");
44             }
45         }.doMethod01();
46         
47 //         doStudyInterface(()->{//代表接口中的那个抽象方法
48 //             System.out.println("CGB1907");
49 //         });
50         
51     }
52 }

 

以上是关于JDK1.8新特性的主要内容,如果未能解决你的问题,请参考以下文章

JAVA 8 主要新特性 ----------------JDK1.8优点概括

JDK1.8新特性

Java8(JDK1.8)新特性

jdk1.8新特性Lambda表达式方法引用

jdk1.8新特性之lambda表达式

jdk1.8新特性