简体   繁体   English

Java“无法取消引用double”错误-double数据类型上的调用方法

[英]Java “double cannot be dereferenced” error - calling method on double data type

I'm trying to use the following method for conversion between temperature scales in Java. 我正在尝试使用以下方法在Java中的温度刻度之间进行转换。 Problem is when I call the method I get a "double cannot be dereferenced" error. 问题是当我调用该方法时,出现"double cannot be dereferenced"错误。

I understand that the reason is that methods cannot be called on primitive data types, and I think the solution is to use a wrapper but I'm not sure what that code would look like (my initial attempts at using Double wrapper all produced errors). 我知道原因是无法在原始数据类型上调用方法,并且我认为解决方案是使用包装器,但是我不确定该代码是什么样(我最初使用Double包装器的尝试都产生了错误) 。

public double celsius(double f) {        
    return (f - 32.0)/1.8; 
}

public double fahrenheit(double c) {        
    return c * 1.8 + 32.0;
}

public void doConversion() {
    double tempC = 0.0;
    double tempF = tempC.fahrenheit; // double cannot be dereferenced error
}

Instead of 代替

double tempF = tempC.fahrenheit;

use 采用

double tempF = fahrenheit(tempC);

Do something like this: 做这样的事情:

public class temperature{
    public double T; //in C

    public temperature(double T){
        this.T = T;
    }

    public double getC(){
        return T;
    }

    public double getF(){
        return T * 1.8 + 32.0;
    }
}

You already have the methods defined in you class. 您已经在类中定义了方法。 While doing conversion, you just have to call those methods like below: 进行转换时,只需调用如下方法:

public void doConversion() {
    double tempC = 0.0;
    double tempF = fahrenheit(tempC);
}

Hope this helps! 希望这可以帮助!

For your code to work, you need to: 为了使代码正常工作,您需要:

public void doConversion() {
    double tempC = 0.0;
    double tempF = fahrenheit(tempC);  // call your function
}

The reason double tempF = tempC.fahrenheit; 原因double tempF = tempC.fahrenheit; is a problem is that the . 一个问题是. (ie dot) is meant to allow classes and objects to access their member fields and methods. (即点)旨在允许类和对象访问其成员字段和方法。

A variable of type double is a primitive type; 类型为double的变量是原始类型; it's not a class or an object, so the dot operator cannot be used with them. 它不是类或对象,因此点运算符不能与它们一起使用。

The wording in the messaging is because classes and their instances (ie objects) are always references . 消息传递中的用语是因为类及其实例(即对象)始终是引用 In other words, if you do something like: 换句话说,如果您执行以下操作:

List<DisplayMode> displayModes = new ArrayList<>();

the variable displayModes holds a reference (ie points to) the list, but is not the list itself. 变量displayModes拥有列表的引用( displayModes向该列表),但不是列表本身。 You can then do something like displayModes.add(new DisplayMode(...)); 然后,您可以执行诸如displayModes.add(new DisplayMode(...));

Notice you need to use the new operator with classes, but not with primitives. 注意,您需要将new运算符与类一起使用,而不必与基元一起使用。

On the other hand, if you write: 另一方面,如果您写:

int x = 5;

the variable x is the value itself. 变量x是值本身。

If you really want to use the dot operator, then consider something like: 如果您真的想使用点运算符,请考虑以下内容:

Temperature temp = new Celsius(0.0);
System.out.println(temp.toFahrenheit().toString());

Assuming that you have an interface called Temperature with at least a method called toFahrenheit() and concrete classes Celsius and Fahrenheit implementing said interface. 假设您有一个名为Temperature接口 ,至少有一个名为toFahrenheit()的方法,而具体的类CelsiusFahrenheit 实现了该接口。

public interface Temperature {
    Temperature toFahrenheiht();
}

public class Celsius implements Temperature {
    // ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM