现在的位置: 首页 > 综合 > 正文

替换switch case方法的方式1–多态

2013年10月02日 ⁄ 综合 ⁄ 共 244字 ⁄ 字号 评论关闭
double getSpeed() {
    switch (_type) {
      case EUROPEAN:
        return getBaseSpeed();
      case AFRICAN:
        return getBaseSpeed() - getLoadFactor() * _numberOfCoconuts;
      case NORWEGIAN_BLUE:
        return (_isNailed) ? 0 : getBaseSpeed(_voltage);
    }
    throw new RuntimeException ("Should be unreachable");
  }

抱歉!评论已关闭.