繁体   English   中英

使用类引用调用方法时出现“找不到符号”错误

[英]"cannot find symbol" error when invoking a method using reference of class

我觉得这似乎非常简单,但我无法让它发挥作用。 我正在尝试使用这个:

import java.util.Scanner;
import java.lang.Math;
public class SammysRentalPriceWithMethods
{

public static void main(String[] args)
{
Rental rental = new Rental();
SammysRentalPriceWithMethods SRPWM = new SammysRentalPriceWithMethods();
getLogo();
getContractNum();
getHoursAndMinutes();
}

public static void getLogo()
{
rental.setlogo();
}

public static void getContractNum()
{
rental.setContractNumber();
}

public static void getHoursAndMinutes()
{
rental.setHoursAndMinutes();
}

}

调用这个类和里面包含的方法:

import java.util.Scanner;
import java.lang.Math;
public class Rental
{
public final int minutes = 60;
public final double hourlyRate = 40.0;
private static String contractNum;
private static double hours;
private static int minutes2;
private static double price;
Scanner Input = new Scanner(System.in);

public static void setlogo()
{
System.out.println("*********************************");
System.out.println("*Sammy's makes it fun in the sun*");
System.out.println("*********************************");
}

public static void setContractNumber()
{
Scanner Input = new Scanner(System.in);
System.out.println("Please enter your contract number.");
contractNum = Input.nextLine();
}

public static void setHoursAndMinutes()
{
int timeOver;
Scanner Input2 = new Scanner(System.in);
System.out.println("Please enter the amount of time in minutes you rented the equipment.");
minutes2 = Input2.nextInt();
if (minutes2 > 60)
{hours = (minutes2/60);
price = (hours * 40);
timeOver = (minutes2%60);
price = (price + timeOver);
System.out.println("You rented the equipment for " + hours + " hours and " + timeOver + " minutes.");
System.out.println("Your total price is: " + price);
}
else if (minutes2 < 60)
{
price = (minutes2 * 1);
System.out.println(price);
}
}
}

但是编译器在 SRPWM 类中的每个租用引用上都说“错误:找不到符号”。 我已经在 main 方法中调用了这个类。 有任何想法吗?

编译器是对的。

变量rental 和SRPWM 的范围仅限于main 方法。 要么将属性传递给类的方法,要么将它们设为 SammysRentalPriceWithMethods 的静态字段。

问题是您混合了static和非static成员并在您的SammysRentalPriceWithMethods类中调用它们,因此更改类,如下面的代码所示并带有注释:

public class SammysRentalPriceWithMethods {

    private Rental rental;

    //Use constructor to inject Rental object
    public SammysRentalPriceWithMethods(Rental rental) {
        this.rental = rental;
    }

    public static void main(String[] args) {
        //create Rental object
        Rental rental = new Rental();

        SammysRentalPriceWithMethods srpwm =new SammysRentalPriceWithMethods();

        //invoke methods using srpwm reference
        srpwm.getLogo();
        srpwm.getContractNum();
        srpwm.getHoursAndMinutes();
    }

    public void getLogo() {
        rental.setlogo();
    }

    public void getContractNum() {
                rental.setContractNumber();
    }

    public void getHoursAndMinutes() {
                rental.setHoursAndMinutes();
    }
}

您需要记住以下基础知识:

(1) 使用类名和 调用类的静态成员. 运算符(如果你想在静态方法中调用静态成员,你可以在没有类名和 . 的情况下调用它们

(2) 使用对象和 调用非静态成员. 操作员

(3) 变量名、方法名以驼峰命名(首字母小写)

因为rental是在你的main方法中声明的,所以它只会在那个方法中可见。 您应该考虑在类级别声明此变量。

您需要在main()函数之外声明Rental类变量。 如果您在main() 中声明它,那么您将无法在其他函数中使用它。 因此,让您的Rental变量成为全局变量。

新文件应该是这样的:

import java.util.Scanner;
import java.lang.Math;

public class SammysRentalPriceWithMethods {

Rental rental = new Rental();

public static void main(String[] args) { 
SammysRentalPriceWithMethods SRPWM = new SammysRentalPriceWithMethods();

getLogo(); 
getContractNum();
getHoursAndMinutes(); 
}

 public static void getLogo() { 
     rental.setlogo(); 
 } 

 public static void getContractNum() { 
     rental.setContractNumber();
 } 

public static void getHoursAndMinutes() { 
    rental.setHoursAndMinutes();
} 

}

暂无
暂无

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

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