Java 这意味着什么(此方法将返回另一个单位中的新温度对象,而不更改当前对象。)

Java 这意味着什么(此方法将返回另一个单位中的新温度对象,而不更改当前对象。),java,Java,这是我的提示: 要创建温度转换器,我想知道OtherUnit中返回newTemp对象意味着什么 实现一个名为Temperature的类,该类表示以华氏或摄氏为单位的温度。允许度数有小数位(例如双精度),并使用枚举存储单位。使用可能的摄氏度、华氏度和开尔文值命名枚举TemperatureUnit。具体而言,温度类具有以下实例变量(也称为字段或数据): 度数(双精度) 单元(TemperatureUnit-来自您创建的枚举) 温度等级将具有以下方法: 创建新温度(给定度和单位值)[参数化构造函数]

这是我的提示:

要创建温度转换器,我想知道
OtherUnit
中返回
newTemp
对象意味着什么

实现一个名为Temperature的类,该类表示以华氏或摄氏为单位的温度。允许度数有小数位(例如双精度),并使用枚举存储单位。使用可能的摄氏度、华氏度和开尔文值命名枚举
TemperatureUnit
。具体而言,温度类具有以下实例变量(也称为字段或数据):

度数
(双精度)
单元
(TemperatureUnit-来自您创建的枚举)

温度等级将具有以下方法:

创建新温度
(给定度和单位值)[参数化构造函数]
创建一个新温度
(不提供任何参数-将温度初始化为0.0摄氏度)[默认构造函数]
创建新温度
(从另一个温度)[复制构造函数]

getDegrees

getUnit

setDegrees

setUnit
convertTo
(TemperatureUnit Other unit)-此方法将一个单位中的度数转换为另一个单位中的度数(例如,华氏度到摄氏度),如果进行转换,则返回true,否则返回false(唯一不进行转换的方法是单位相同(例如,华氏度到华氏度)
inOtherUnit
(TemperatureUnit otherUnit)-此方法将在不更改当前对象的情况下返回另一个单位中的新温度对象

这是我已有的代码:

public class Temperature {

    private double degrees;
    private TempUnit unit;

    public Temperature(double degrees, TempUnit unit) {
        this.degrees = degrees;
        this.unit = unit;
    }

    // default constructor
    public Temperature() {
        this.degrees = 0.0;
        this.unit = TempUnit.Celsius;
    }

    public Temperature(Temperature copyTemperature) {
        this.degrees = copyTemperature.degrees;
        this.unit = copyTemperature.unit;
    }

    public double getDegrees() {
        return this.degrees;
    }

    public TempUnit unit() {
        return this.unit;
    }

    public void setDegrees(double newDegrees) {
        this.degrees = newDegrees;
    }

    public boolean convertTo(TempUnit otherUnit) {
        if (this.unit == otherUnit) {
            return false;
        }
        if (this.unit == TempUnit.Celsius && otherUnit == TempUnit.Fahrenheit) {
            this.degrees = this.degrees * (9.0 / 5.0) + 32.0;
        } else if (this.unit == TempUnit.Celsius && otherUnit == TempUnit.Kelvin) {
            this.degrees += 273.15;
        } else if (this.unit == TempUnit.Fahrenheit && otherUnit == TempUnit.Celsius) {
            this.degrees = (this.degrees - 32.0) * 5.0/9.0;
        } else if (this.unit == TempUnit.Fahrenheit && otherUnit == TempUnit.Kelvin) {
            this.degrees = (this.degrees + 459.67) * 5.0/9.0;
        } else if (this.unit == TempUnit.Kelvin && otherUnit == TempUnit.Celsius) {
            this.degrees = this.degrees - 273.15;
        } else if (this.unit == TempUnit.Kelvin && otherUnit == TempUnit.Fahrenheit) {
            this.degrees = (this.degrees * 9.0/5.0) - 459.67;
        }
        this.unit = otherUnit;
        return true;
    }

    public boolean another(TempUnit otherUnit) {
        if (this.unit == otherUnit) {
            return false;
        }
        else
            return true;
    }

本质上,方法
otherUnit
将需要返回一个不同的
Temperature
对象,该对象将与其他温度单位中的等效温度相同。
otherUnit
将本质上是一个转换器,也就是说,给定对象的当前状态,它将返回一个具有不同值但不相同的新对象相当于当前的一个

因此,表示0摄氏度温度的对象可以转换为另一个32华氏度的对象,然后再转换为272开尔文的对象。这意味着,尽管对象具有不同的值,但根据上下文(即温度值和单位),它们都是等效的

本质上,这意味着:

public Temperature otherUnit(TempUnit unit) {
    switch(unit) {
        case Celcius: {
            //If the current temperature is in Celcius, then return the current object.
            if(this.unit == TempUnit.celcius) return this;            
            // return a new object which has the equivalent temperature in Celcius.
        }
        ...    
    }
}

我认为最好在开始时进行检查,并且更通用:
f(this.unit==unit)返回这个;