模板模式
举例:
public abstract class SpeedMeter
{
//转速
private double turnRate;
public SpeedMeter()
{
}
//把返回车轮半径的方法设置成抽象方法,交给其子类实现
public abstract double getRadius();
public void setTurnRate(double turnRate)
{
this.turnRate = turnRate;
}
//定义计算速度的通用算法
public double getSpeed()
{
//速度等于 车轮半径 * 2 * PI * 转速
return java.lang.Math.PI * 2 * getRadius * turnRate;
}
}
public class CarSpeedMeter extends SpeedMeter
{
@Override
public double getRadius()
{
return 0.28;
}
public static void main(String[]args)
{
CarSpeedMeter csm = new CarSpeedMeter();
csm.setTurnRate(15);
System.out.println(csm.getSpeed());
}
}