JDK7 JDK8 JDK9接口中的默认方法、静态方法、私有方法

打印 上一主题 下一主题

主题 542|帖子 542|积分 1626

JDK8开始之后接口新增的方法
JDK7从前:接口中只能定义抽象方法
JDK8的新特性:接口中可以定义有方法体的方法(默认、静态)
JDK9的新特性:接口中可以定义私有方法
  1. <strong> 接口中的默认方法
  2. </strong>
  3. InterA
复制代码
  1. package com.itheima.a06;
  2. public interface InterA {
  3.      // 抽象方法 不能拥有方法体
  4.     public abstract void show();
  5.     // public 可以省略 default不可以省略
  6.     public default void print() {
  7.         System.out.println("A接口中的默认方法---print");
  8.     }
  9. }
复制代码
  1. InterB
复制代码
  1. package com.itheima.a06;
  2. public interface InterB {
  3.      // 抽象方法 不能拥有方法体
  4.     public abstract void show();
  5.     // public 可以省略 default不可以省略
  6.     public default void print() {
  7.         System.out.println("B接口中的默认方法---print");
  8.     }
  9. }
复制代码
  1. intermpl
复制代码
  1. package com.itheima.a06;
  2. public class intermpl implements InterA,InterB {
  3.     @Override
  4.     public void show() {
  5.         System.out.println("实现类 重写的抽象方法");
  6.     }
  7.     // 默认方法不是抽象方法 不强制重写 如果重写 去掉default关键字
  8.     // 实现多个接口 多个接口存在相同名字的默认方法,子类就必须对该方法重写
  9.     @Override
  10.     public void print() {
  11.         System.out.println("重写接口中的默认方法");
  12.     }
  13. }
复制代码
  1. Test
复制代码
  1. package com.itheima.a06;
  2. public class Test {
  3.     public static void main(String[] args) {
  4.         intermpl ii=new intermpl();
  5.         ii.show();
  6.         ii.print();
  7.     }
  8. }
复制代码
JDK8以后新增的静态方法

  1. Inter
复制代码
  1. package com.itheima.a07;
  2. public interface Inter {
  3.     public abstract void method();
  4.     // 静态方法不能被重写
  5.     public static void show(){
  6.         System.out.println("Inter方法中的静态方法");
  7.     };
  8. }
复制代码
  1. InterImpl
复制代码
  1. package com.itheima.a07;
  2. public class InterImpl implements Inter {
  3.     @Override
  4.     public void method() {
  5.         System.out.println("InterImpl中重写的抽象方法");
  6.     }
  7.     // 不叫重写
  8.     public static void show(){
  9.         System.out.println("InterImpl方法中的静态方法");
  10.     };
  11. }
复制代码
  1. Test
复制代码
  1. package com.itheima.a07;
  2. public class Test {
  3.     public static void main(String[] args) {
  4.         // 调用接口中的静态方法
  5.         Inter.show();
  6.         // 调用实现类中的静态方法
  7.         InterImpl.show();
  8.     }
  9. }
复制代码
JDK9以后的私有方法

  1. InterA
复制代码
  1. package com.itheima.a08;
  2. public interface InterA {
  3.     public default void show1(){
  4.         System.out.println("show1方法执行");
  5.         show3();
  6.     };
  7.     public default void show2(){
  8.         System.out.println("show2方法执行");
  9.         show3();
  10.     };
  11.     //普通私有方法,给默认方法服务
  12.     private void show3(){
  13.         System.out.println("记录细节的代码");
  14.     };
  15.     public static void show11(){
  16.         System.out.println("show1方法执行");
  17.         show4();
  18.     };
  19.     public static void show22(){
  20.         System.out.println("show2方法执行");
  21.         show4();
  22.     };
  23.     //静态私有方法,给静态方法服务
  24.     private static void show4(){
  25.         System.out.println("记录细节的代码");
  26.     };
  27. }
复制代码
总结


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

不到断气不罢休

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表