java中的接口概念

接口的特点:

  接口中只有抽象方法和全局常量

public interface className{}

 范例:简单的代码模型

interface A{
    public static final String str = "Hello";
    public abstract void print(); //如果不加public abstract 也会默认是public类型的抽象方法
}
interface B{
    public void fun();
}
interface D{
    void get();
}
class C implements A,B,D{ // 接口允许多继承
    public void print(){
        System.out.println("a中的抽象方法");
    }
    public void fun(){
        System.out.println("b中的抽象方法");
    }
    public void get(){
        System.out.println("d中的抽象方法");
    }
}
public class Test{
    public static void main(String args[]){
        A a = new C();    //向上转型
        B b= new C();
        D d = new C();
        a.print();
        b.fun();
        d.get();
    }
}

如果接口继承接口则用extends

接口的核心作用:

  1.定义不同层之间的标准

  2.表示一种操作的能力

  3.将服务器端的远程方法暴露给客户端

 

原文地址:https://www.cnblogs.com/hu1056043921/p/7327524.html