zoukankan      html  css  js  c++  java
  • 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));
      }
    }
    
  • 相关阅读:
    蘑菇街
    康拓展开
    CSS学习笔记
    专业名词
    专业名字
    01背包问题
    将bbr功能合入到centos7.3
    How to Identify User&Password of DataBase safely in SQL statement?
    tips for private constructor
    all Key Word of C#
  • 原文地址:https://www.cnblogs.com/weiyining/p/13073715.html
Copyright © 2011-2022 走看看