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