模式
public interface Shape{
public double getArea(){}
public double getVolume(){}
public String getName(){}
}
为使用接口,必须实现该接口的类。(如果一个类没有实现任何接口方法,则它是抽象类,并且必须以关键字abstract声明该类)
public class Point implements Shape{
…
//实现接口类方法
public double getArea(){
return …;
}
public double getVolume(){
return…;
}
public String getName(){
return…;
}
}
实现接口的好处:
-子类可以实现它所需要的认识数目的接口
-声明一组可以用于多个类声明的常量。
public interface Constants{
public static final int ONE= 1;
public static final int TWO= 2;
public static final int THREE= 3;
}
实现该接口的类可以在类声明的任何位置使用ONE,TWO,THREE。或者使用该形式:Constants.ONE 来使用它们。