接口
1、接口默认方法的使用
①接口的默认方法可以通过接口实现类对象直接调用。
②接口的默认方法也可以被接口实现类进行覆盖重写
1 package cn.intcast.demo17; 2 3 public interface MyInterfaceDefault { 4 //抽象方法 5 public abstract void methodAbs(); 6 // void AAA(); 7 public default void methodA(){ 8 System.out.println("this is a default method!"); 9 } 10 }
1 package cn.intcast.demo17; 2 3 public class MyInterfaceDefaultA implements MyInterfaceDefault { 4 @Override 5 public void methodAbs() { 6 System.out.println("实现了抽象 方法,AAA;"); 7 } 8 }
1 package cn.intcast.demo17; 2 3 public class Code01Interface { 4 public static void main(String[] args) { 5 //创建实现类对象 6 MyInterfaceDefaultA s = new MyInterfaceDefaultA(); 7 //调用抽象方法,如果实现类当中没有,会向上找接口 8 s.methodA(); 9 s.methodAbs(); 10 } 11 }
运行结果图
2、接口的静态方法使用
接口中的静态方法可以通过接口名称,调用其中的静态方法
格式:接口名称.静态方法名(参数)
1 package cn.intcast.demo17; 2 3 public interface StaticInterface { 4 public static void staticmethod(){ 5 System.out.println("this is a static method!"); 6 } 7 }
1 package cn.intcast.demo17; 2 3 public class CodeStatic { 4 public static void main(String[] args) { 5 StaticInterface.staticmethod(); 6 } 7 }
运行结果图
3、
原文:https://www.cnblogs.com/Anemia-BOY/p/10747589.html