4

我有不同的类代表不同的单位(体积、重量、距离),带有一个枚举值,用于将转换值存储到基本类型。

问题是有很多代码重复,我确信有一种优雅的方法可以编写一个可以避免它的抽象类。我不知道我应该如何声明这个类。这是卷类:

import java.math.BigDecimal;

import lombok.Data;

@Data
public class Volume {

public enum Unit
{
    CUBIC_METER(new BigDecimal("1")), // CUBIC_METER
    // 1 m3 = 61023.7 inch3
    CUBIC_INCH(new BigDecimal("61023.7"));
    private Unit(BigDecimal m3Value)
    {
        this.m3Value = m3Value;
    }

    public final BigDecimal m3Value;
}
// internally, we store the volume in m3
private final   BigDecimal  volumeInM3;

public Volume()
{
    volumeInM3 = BigDecimal.ZERO;
}

public Volume(final String volumeValue, final Unit volumeUnit)
{
    this(new BigDecimal(volumeValue), volumeUnit);
}

public Volume(final BigDecimal volumeValue, final Unit volumeUnit)
{
    if (volumeValue.signum() == 0)
    {
        volumeInM3 = BigDecimal.ZERO;
    }
    else
    {
        volumeInM3 = volumeValue.divide(volumeUnit.m3Value, NumberUtil.MC);
    }
}

/**
 * Return the volume in the unit given in param
 * @param volumeUnit
 * @return
 */
public BigDecimal getAs(final Unit volumeUnit)
{
    return volumeInM3.multiply(volumeUnit.m3Value, NumberUtil.MC);
}

public Volume add(final Volume volumeToAdd)
{
    BigDecimal newVolumeValue = volumeToAdd.volumeInM3.add(volumeInM3, NumberUtil.MC);
    return new Volume(newVolumeValue, Volume.Unit.CUBIC_METER);
}

public Volume divide(final Volume divisor)
{
    BigDecimal newVolumeValue = volumeInM3.divide(divisor.volumeInM3, NumberUtil.MC);
    return new Volume(newVolumeValue, Volume.Unit.CUBIC_METER);
}

public boolean isZero()
{
    if (volumeInM3.signum() == 0)
        return true;
    return false;
}

public boolean isEqual(final Volume another)
{
    if (volumeInM3.compareTo(another.volumeInM3) == 0)
        return true;
    return false;
}
}

这是非常相似的重量类:

import java.math.BigDecimal;

import lombok.Data;

@Data
public class Weight {

// the value stored with enum is the value used to convert the unit to kilogramm, 
// wich is the reference unit
public enum Unit
{
    KILOGRAM(new BigDecimal("1")),
    // 1 kg = 1000 g 
    GRAM(new BigDecimal("1000")),
    // 1 kg = 2.20462 pounds
    POUND(new BigDecimal("2.20462"));

    private Unit(BigDecimal kgValue)
    {
        this.kgValue = kgValue;
    }

    private final BigDecimal kgValue;
}

// internally, we store the weight inKg
private final BigDecimal weightInKg;

public Weight()
{
    weightInKg = BigDecimal.ZERO;
}

public Weight(final String weightValue, final Unit weightUnit)
{
    this(new BigDecimal(weightValue), weightUnit);
}

public Weight(final BigDecimal weightValue, final Unit weightUnit)
{
    if (weightValue.signum() == 0)
    {
        weightInKg = BigDecimal.ZERO;
    }
    else
    {
        weightInKg = weightValue.divide(weightUnit.kgValue, NumberUtil.MC);
    }
}

/**
 * Return the weight in the unit given in param
 * @param weightUnit
 * @return
 */
public BigDecimal getAs(final Unit weightUnit)
{
    return weightInKg.multiply(weightUnit.kgValue, NumberUtil.MC);
}

public Weight add(final Weight weightToAdd)
{
    BigDecimal newWeightValue = weightToAdd.weightInKg.add(weightInKg, NumberUtil.MC);
    return new Weight(newWeightValue, Weight.Unit.KILOGRAM);
}

public Weight divide(final Weight divisor)
{
    BigDecimal newWeightValue = weightInKg.divide(divisor.weightInKg, NumberUtil.MC);
    return new Weight(newWeightValue, Weight.Unit.KILOGRAM);
}
public boolean isZero()
{
    if (weightInKg.signum() == 0)
        return true;
    return false;
}

public boolean isEqual(final Weight another)
{
    if (weightInKg.compareTo(another.weightInKg) == 0)
        return true;
    return false;
}
}

当实例化一个卷时,用户被迫明确地提供单位。

Volume myCubicMeter, myCubicInch;
myCubicMeter = new Volume("1", Volume.Unit.CUBIC_METER);
myCubicInch = new Volume("1", Volume.Unit.CUBIC_INCH);

我想要实现的是一个抽象类,它将实现所有方法并强制子类用值实现枚举。这样做的正确方法是什么?

4

1 回答 1

1

在 Java 中,枚举是作为所提供类的子类的Enum类。

你不能给它们一个共同的抽象祖先,你也不能将它们子类化。如果您真的想保留从一组枚举中使用编译时间常数来引用您的单元的商品,那么您将无法做到这一点。

如果您放弃使用枚举并可能将它们声明为普通类,那么您可以像使用其他所有内容一样自由地进行操作,例如

class VolumeUnit extends AbstractUnit {
  public static final VolumeUnit CUBIC_METER = new VolumeUnit(params);
  ..

  VolumeUnit(..) {
    ..
  }
}
于 2012-10-01T22:40:53.800 回答