JAVA——接口中的静态方法和默认方法 接口体 接口中的静态方法 接口中的默认方法

interface 接口名
{
  常量
  抽象方法
  静态方法
  默认方法
}

接口中的静态方法

  • 不能被子接口继承
  • 不能被实现该接口的类继承
  • 调用形式:接口名.静态方法名()
interface Face
{
  final static double PI = 3.14;
  public static String show( )
  {
    return "我是Face接口中的静态方法";
  }
}
public class Test implements Face
{
  public static void main(String[ ] args)
  {
    System.out.println( Face.show( ) );
  }
}

接口中的默认方法

  • 可以被子接口继承
  • 可以被实现该接口的类继承
  • 子接口中如有同名默认方法,父接口中的默认方法会被覆盖
  • 不能通过接口名调用
  • 需要通过接口实现类的实例进行访问
  • 调用形式:对象名.默认方法名()
interface Face
{
  final static double PI = 3.14;
  public default double area(int r)
  {
    return r*r*PI;
  }
}
public class Test implements Face
{
  public static void main(String[ ] args)
  {
    Test t1 = new Test( );
    System.out.println("面积为:"+ t1.area(3));
  }
}