找不到方法的符号

本文关键字:符号 方法 找不到 | 更新日期: 2023-09-27 18:30:43

我以前从未遇到过这个问题,我不知道它为什么要这样做。我正在尝试调用方法.calcTonsCO2();但它给了我一个找不到符号错误。我知道该方法存在,我没有打错字......怎么回事

主要方法测试仪:

public class CO2FootprintV1Tester
{
    public static void main (String[] args)
    {
        //declaration of variables
        double gals, tons, pounds;
        //initialization        
        CO2FootprintV1Tester footprint = new CO2FootprintV1Tester();
        //methods
        footprint.calcTonsCO2();
        footprint5.convertTonsToPoundsCO2();
        tons = footprint.getTonsCO2();
        pounds = footprint.getPoundsCO2();
    }
}

主方法类:

public class CO2FootprintV1
{   
    //declaration of private instance variables
    private double myGallonsUsed; 
    private double myTonsCO2;
    private double myPoundsCO2;
    /**
     * Constructor for ojbects of type CO2FootPrintV1
     * @param gals are gallons used
     * 
     */
    CO2FootprintV1 (double gals)
    {        
        myGallonsUsed = gals;
    }
    /**
     *  Method to calculate tons of CO2
     */
    public void calcTonsCO2()
    {
        myTonsCO2 = (8.78 * Math.pow(10 , -3)) * myGallonsUsed;        
    }
    /**
     * method to convert TOns to Pounds
     */
    public void convertTonsToPoundsCO2()
    {
        myPoundsCO2 = myGallonsUsed * 2204.62;
    }
    /**
     * Method to get the MyTonsCO2 private instance
     */
    public double getTonsCO2()
    {
        return myTonsCO2;
    }
    /**
     * Method to get the MyPoundsCO2 private instance
     */
    public double getPoundsCO2()
    {
        return myPoundsCO2;
    }
}

找不到方法的符号

好吧,

不确定您使用的是哪种特定的编程语言,但是calcTonsCO2()方法CO2FootprintV1类上定义,但您正在尝试在类的实例上调用它CO2FootprintV1Tester

public class CO2FootprintV1
 {
    .......
public void calcTonsCO2()
{
    myTonsCO2 = (8.78 * Math.pow(10 , -3)) * myGallonsUsed;
}
  ....
}

您正在呼叫

CO2FootprintV1Tester footprint = new CO2FootprintV1Tester();
footprint.calcTonsCO2();

它应该是

CO2FootprintV1 footprint = new CO2FootprintV1();
footprint.calcTonsCO2();