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();
}
}