无法解析温度驱动程序 Java class 中的方法

Can't resolve methods in Java class for Temperature Driver

***我在正确创建 add() subtract() 和 divide() 方法时遇到问题,也许我想多了。我将如何正确 return 需要的对象。我的 IDE 是 "Cannot resolve symbol Temperature"。我调用 'this' 正确吗?

***我知道 add 和 divide 方法不完整,如果我能在完成 subtract() 方法时获得帮助,一切都应该到位。

    import javax.naming.StringRefAddr;

/**
 * Created by Makoto on 2/4/2016.
 */

public class Temperature {
    public double temp;
    public char type;

    Temperature() {
        temp = 0.0;
        type = 'o';

    }

    Temperature( double temperature, char tempType){
            temperature = temp;
            tempType = type;
    }

    public Temperature toFahrenheit() {
        type.equalsIgnoreCase();
        switch (type) {
            case 'C':

                break;

            case 'K':

                break;

            default:
                System.out.println("Can not convert.");
                break;
        }

        return this.Temperature;
    }

    public Temperature toCelsius() {
        type.equalsIgnoreCase();
        switch (type) {
            case 'F':

                break;

            case 'K':

                break;

            default:
                System.out.println("Can not convert.");
                break;
        }

        return this.Temperature;
    }

    public Temperature toKelvin() {
        type.equalsIgnoreCase();
        switch (type) {
            case 'C':

                break;

            case 'F':

                break;

            default:
                System.out.println("Can not convert.");
                break;
        }

        return this.Temperature;
    }

    public Temperature add(Temperature x){

        this.Temperature + x;
        return this.Temperature;
    }

    public Temperature subtract(Temperature x){

        this.Temperature = this.temp - x.temp;
        return this.Temperature;
    }
    public Temperature divide(int x){

        this.Temperature = /x;
        return this.Temperature;
    }

}

你只需要return这个。

  import javax.naming.StringRefAddr;

/**
 * Created by Makoto on 2/4/2016.
 */

public class Temperature {
    public double temp;
    public char type;

    Temperature() {
        temp = 0.0;
        type = 'o';

    }

    Temperature( double temperature, char tempType){
            temperature = temp;
            tempType = type;
    }

    public Temperature toFahrenheit() {
        type.equalsIgnoreCase();
        switch (type) {
            case 'C':

                break;

            case 'K':

                break;

            default:
                System.out.println("Can not convert.");
                break;
        }

        return this;
    }

    public Temperature toCelsius() {
        type.equalsIgnoreCase();
        switch (type) {
            case 'F':

                break;

            case 'K':

                break;

            default:
                System.out.println("Can not convert.");
                break;
        }

        return this;
    }

    public Temperature toKelvin() {
        type.equalsIgnoreCase();
        switch (type) {
            case 'C':

                break;

            case 'F':

                break;

            default:
                System.out.println("Can not convert.");
                break;
        }

        return this;
    }

    public Temperature add(Temperature x){

        this.temp + x.temp;
        return this;
    }

    public Temperature subtract(Temperature x){

        this.temp = this.temp - x.temp;
        return this;
    }
    public Temperature divide(int x){

        this.temp = /x;
        return this;
    }

}

this.Temperature = this.temp - x.temp; 不是 Java 中的有效语句。您正在尝试将本身不是对象的 Temperature 设置为一个值。您在这里要做的是对 temp 变量执行操作,然后对 return this 执行操作。通过 returning this,调用对象现在将拥有带有更新字段的 Temperature 对象。

例如:

public Temperature subtract(Temperature x){ temp = temp - x.temp; return this; }

以上代码将更新此温度对象的温度,然后return本身。然后,调用对象可以通过调用 returnedTemperatureObject.temp.

来访问它