接口講解

1.接口的誕生:

             學過c,c++的應該知道C語言是面向過程的,C++是面向對象的,面向對象的一個特徵:多態;(面向對象的語言都支持的三個概念:封裝,繼承,多態),C++實現多態的方法就是繼承,C++支持一個類繼承多個類,但對於Java語言其中有一個特點就是隻能繼承一個類,不能繼承多個類,而爲了解決這個問題,接口就產生了。因此,接口的一個特性就是一個類可以實現多個接口。


2.什麼是接口:

             接口這種技術主要用來描述類具有什麼功能,而並不給出每個功能的具體實現。(好比喻工作時間表,規定了你在某時刻應該幹什麼,類實現接口相當於,你在這時刻幹了什麼)

類實現接口使用:implements
接口的格式:

[public]interface 接口名稱 [extends 父接口列表]
{
[public ] [static][final] 數據類型 成員變量=常量;
[public][abstract]返回值的數據類型 方法名(參數表);
}
2.1 接口的舉例

public interface Cat {
    public static final int number=100;
    public abstract void run();
//  public default void print()
//  {
//      System.out.println("在接口中實現默認的方法");
//  }

}

3 接口的特性

  1. 接口和抽象類一樣不能被實例化。cat=new Cat()//error 然而,接口不能構造接口的對象卻能聲明接口的變量。如Cat c。
  2. 接口中的變量時靜態常量 如果寫int number=100;默認的是public static final ;接口中的方法時抽象的方法,不寫默認是public abstract。
  3. jdk8 接口可以實現默認的方法如
    public default void print()
    {
    System.out.println("在接口中實現默認的方法");
    }
  4. 接口可以繼承多個接口(extends)如:
    public interface Name extends Me,Me2{
    public static final int i=10;
    abstract public   void  add();
    }

    5.實現接口的類必須要覆寫接口中的全部抽象方法。
    6.接口沒有構造函數的


4 接口的實現

1 對於要實現接口的類,可以用implements 關鍵字來實現接口。
例子:
interface1接口:

public interface Interface1 {
    public final static String name="interface1";
    public abstract void print1();
    public default void print()
    {
        System.out.println("interface1的實現默認方法");
    }

}

實現接口的類:

public class InterfaceImpl implements Interface1{

    @Override
    public void print1() {
        // TODO Auto-generated method stub
        System.out.println("impl interface1");
    }
    public static void main(String[] args) {
        InterfaceImpl impl=new InterfaceImpl();
        //重寫接口的方法
        impl.print1();
        //接口的默認方法
        impl.print();
        //接口的名稱
        System.out.println(Interface1.name);
    }
}

運行結果:

impl interface1
interface1的實現默認方法
interface1

2 接口的對象可以利用子類對象的向上轉型進行實例化。(如果接口對象要調用子類自己的方法,必須強制類型轉換。否則接口對象只能調用自己定義的方法在子類的實現。)

public class InterfaceImpl implements Interface1{

    public void add()
    {
        System.out.println("子類自己的方法");
    }

    @Override
    public void print1() {
        // TODO Auto-generated method stub
        System.out.println("impl interface1");
    }
    public static void main(String[] args) {
//      InterfaceImpl impl=new InterfaceImpl();
//      //重寫接口的方法
//      impl.print1();
//      //接口的默認方法
//      impl.print();
//      //接口的名稱
//      System.out.println(Interface1.name);
        Interface1 inter1=new InterfaceImpl();
        inter1.print();
        inter1.print1();
        System.out.println(Interface1.name);
        //inter1.add();//error
        ((InterfaceImpl) inter1).add();
    }
}

結果:

interface1的實現默認方法
impl interface1
interface1
子類自己的方法
接口的繼承

接口繼承(extends):
interface2 :

public interface Interface2 {
    public final static String name="interface2";
    public abstract void print2();
    public default void print()
    {
        System.out.println("interface2的實現默認方法");
    }

}

interface3:


public interface Interface3 {
    public final static String name="interface3";
    public abstract void print3();
    public default void print()
    {
        System.out.println("interface3的實現默認方法");
    }

}

interface1:

public interface Interface1 extends Interface2,Interface3{
    public final static String name="interface1";
    public abstract void print1();
    public default void print()
    {
        System.out.println("interface1的實現默認方法");
    }

}

實現interface1的類:

public class InterfaceImpl implements Interface1{

    public void add()
    {
        System.out.println("子類自己的方法");
    }

    @Override
    public void print1() {
        // TODO Auto-generated method stub
        System.out.println("impl interface1");
    }
    public static void main(String[] args) {
//      InterfaceImpl impl=new InterfaceImpl();
//      //重寫接口的方法
//      impl.print1();
//      //接口的默認方法
//      impl.print();
//      //接口的名稱
//      System.out.println(Interface1.name);
        Interface1 inter1=new InterfaceImpl();
        inter1.print();
        inter1.print1();
        System.out.println(Interface1.name);
        //inter1.add();//error
        ((InterfaceImpl) inter1).add();
    }

    @Override
    public void print2() {
        // TODO Auto-generated method stub
        System.out.println("interface2");
    }

    @Override
    public void print3() {
        // TODO Auto-generated method stub
        System.out.println("interface3");
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章