java第八章(3)介面

來源:互聯網
上載者:User

8.3介面

介面的結構與抽象類別非常相似.

不同點:

介面的資料成員必須初始化;

介面中的方法必須全部都聲明為abstract 的,也就是說,介面不能像抽象類別一樣擁有一般的方法,而必須全部是抽象方法.

1).介面的定義

介面的文法格式如下:

[ public ] interface 介面名稱 [ extends 父介面名列表 ]

{

  [ public ][ static ][ final ] 資料類型 成員變數名 = 常量;

    ... ...

  [ public ][ abstract ] 傳回值的資料類型 方法名( 參數表 );

    ... ...

} 

介面的成員變數一定要賦初值,且不能修改,若省略修飾符,系統預設為 public static final;而其成員方法必須是抽象的,所以介面中不可以有一般的方法.

一是介面裡的"抽象方法"只需做聲明,不用定義其處理資料的方法體,二是成員變數必須賦初值.

2).介面的實現與引用

介面實現的文法格式:

class 類名稱 implements 介面名表

{

  ... ...

}

eg:利用形狀介面IShape 建造類.

interface IShape    //定義介面

{

  final double pi = 3.14;

  abstract double getArea( );    //聲明抽象方法

  abstract double getLength( );    //聲明抽象方法

}

class Circle implements IShape    //以IShape 介面來實現Circle 類

{

  double radius;

  public Circle( double r )

  {

    radius = r;

  }

  public double getArea( )    //實現介面中的 getArea( ) 方法

  {

    return pi * radius * radius;

  }

  public double getLength( )    //實現介面中的 getLength() 放法

  {

    return 2 * radius * pi;

  }

}

class Rectangle implements IShape    //用介面IShape 來實現Rectangle 類

{

  private double width;

  private double height;

  public Rectangle( double w; double h )

  {

    width = w;

    height = h;

  }

  public double getArea( )    //實現介面中的 getArea() 方法

  {

    return width * height;

  }

  public double getLength( )    //實現介面中的 getLength() 方法

  {

    return 2 * ( width + length );

  }

}

public class app8_11

{

  public static void main( String [ ] args )

  {

   IShape circle = new Circle( 5.0 );

  System.out.print("圓面積=" + circle.getArea() );

  System.out.println( "圓周長=" + circle.getLength() );

  Rectangle rect = new Rectangle( 6.5, 10.8 );

  System.out.print( "矩形面積=" + rect.getArea() );

  System.out.println( "矩形周長 =" + rect.getLength() );

  }

}

3).介面的繼承

與類相似,介面也有繼承性.定義一個介面時可以通過extends 關鍵字聲明該新介面是某個已存在的父介面的派生介面,它將繼承父介面的所有變數與方法.

與類的繼承不同的是,一個介面可以有一個以上的父介面,他們之間用逗號分隔,形成父介面列表.新介面將繼承所有父介面中的變數與方法.如果子介面中定義了與父介面同名的常量或者相同的方法,則父介面中的常量被隱藏,方法被重載.

eg:介面的繼承.

interface Face1   //定義介面 Face1

{

  double pi = 3.14;

  abstract double area();

}

interface Face2

{

  abstract void setColor( String c );

}

interface Face3 extends Face1,Face2

{

  abstract void volume();

}

public class Cylinder implements Face3

{

  private double radius;

  private int height;

  protected String color;

  public Cylinder( double r, int h )

  {

    redius = r;

    height = h;

  }

  public double area()

  {

    return pi * radius * radius;

  }

  public void setColor( String c )

  { 

    color = c;

    System.out.println( "顏色: " + color );

  }

  public void volume()

  {

    System.out.println( "圓柱體體積=" + area() * height );

  }

  public static void main( String [] args )

  {

    Cylinder volu = new Cylinder(3.0, 2);

    volu.setColor( "紅色" );

    volu.volume();

  }

}

 

4).利用介面實現多重繼承

一個類只能有一個直接父類,但是它可以同時實現若干個介面.一個類實現多個介面時,在 implements 子句中用逗號分隔各個介面名.這種情況下如果把介面理解成特殊的類,那麼這個類利用介面實際上就獲得了多個父類,即實現了多重繼承.

eg:利用介面實作類別的多重繼承.   其實在上一例題裡已經使用了多重繼承...

interface Face1

{

  double pi = 3.14;

  abstract double area();

}

interface Face2

{

  abstract void volume();

}

public class Cylinder implements Face1,Face2

{

  private double radius;

  private int height;

  public Cylinder ( double r, int h )

  {

    radius = r;

    height = h;

  }

  public double area()

  {

    return pi*radius * radius;

  }

  public void volume()

  {

    System.out.println( "圓柱體體積=" + area() * height );

  }

  public static void main( String [] args )

  {

    Cylinder volu = new Cylinder( 5.0, 2 );

    volu.volume();

  }

}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.