Java基础之JDK1.8的接口新特性

在Java的JDK1.8中:

  1、接口中使用default来修饰普通方法与使用static来修饰普通方法的意义用于避免子类重复实现同样的代码

 1 package test;
 2 /**
 3  * 泛型的使用
 4  * @author Administrator
 5  *
 6  */
 7 public class TestType {
 8 
 9     public static void main(String[] args) {
10         Msg msg = new MsgImpl();
11         msg.show();
12         msg.fun();
13         Msg.fun2();
14     }
15 }
16 
17 interface Msg{
18     //抽象方法
19     public abstract void show();
20     //使用default定义普通方法
21     default void fun(){
22         System.out.println("在接口中使用default定义的普通方法,用该接口的实现类的对象调用");
23     }
24     //使用static定义的普通方法
25     static void fun2(){
26         System.out.println("在接口中使用static定义的普通方法,用该接口直接调用");
27     }
28 }
29 
30 class MsgImpl implements Msg{
31     @Override
32     public void show() {
33         System.out.println("这是改接口的抽象方法");
34     }
35 }

  2、接口的使用还应该以抽象方法为主;

猜你喜欢

转载自www.cnblogs.com/in-the-game-of-thrones/p/11273624.html