使用变量调用访问器

Calling an Accessor with a Variable

我正在通过为自己做一些基本练习来练习我的 Java 技能,我想做一个愚蠢的简单 'text based race game',我想你可以说。唯一的逻辑是,谁的车更强大,谁就赢,而我在这里要学习的重点是 OOP。我还有其他 4 个 类(Supra、RX7、MX5 和 S2000)并调用它们。在执行其背后所有逻辑的方法 'race' 中,我将用户选择的汽车设置为名为 'userCar' 的变量。我试图在 userCar 上调用我的 getHorsepower() 访问器,这样我就可以计算出谁会赢。我的意图是,如果 'userCar' 是 'supra',那么 'userCar.getHorsepower()' 将与 'supra.getHorsepower()' 相同。但是,我收到一条错误消息,指出在 if 语句中无法解析 getHorsepower() 方法。我相信这是因为我试图在变量上调用我的访问器,如果是这样,我该怎么做才能解决这个问题?或者这可能是一个不同的问题。

谢谢, 纳什

确切错误: 错误:(100, 20) java: 找不到符号 符号:方法 getHorsepower() 位置:java.lang.String

类型的变量 userCar
  import java.util.Scanner;

public class Cars{

    private static Scanner in = new Scanner(System.in);

    private int horsepower;
    private int year;
    private String coo;

    public static void main(String args[])
    {
        Supra supra = new Supra();
        RX7 rx7 = new RX7();
        MX5 mx5 = new MX5();
        S2000 s2000 = new S2000();

        supra.specs();
        rx7.specs();
        mx5.specs();
        s2000.specs();

        System.out.println("Supra: " + supra.getHorsepower() + "HP");
        System.out.println("RX7: " + rx7.getHorsepower() + "HP");
        System.out.println("S2000: " + s2000.getHorsepower() + "HP");
        System.out.println("MX5 (Miata): " + mx5.getHorsepower() + "HP");

        race();
    }

    public void setHorsepower(int h)
    {
        this.horsepower = h;
    }
    public int getHorsepower()
    {
        return horsepower;
    }


    public static void race()
    {
        Supra supra = new Supra();
        RX7 rx7 = new RX7();
        MX5 mx5 = new MX5();
        S2000 s2000 = new S2000();

        supra.specs();
        rx7.specs();
        mx5.specs();
        s2000.specs();

        String userCar = "";
        String compCar = "";

        System.out.println("Choose a car to race");
        System.out.println("1) Supra | 2) RX7 | 3) S2000 | 4) MX5 (Miata)");
        int userChoice = in.nextInt();
        System.out.println("Choose a car to race against");
        int compChoice = in.nextInt();

        if(userChoice == 1)
            userCar = "supra";
        else if(userChoice == 2)
            userCar = "rx7";
        else if(userChoice == 3)
            userCar = "s2000";
        else if(userChoice == 4)
            userCar = "mx5";

        if(compChoice == 1)
            compCar = "supra";
        else if(compChoice == 2)
            compCar = "rx7";
        else if(compChoice == 3)
            compCar = "s2000";
        else if(compChoice == 4)
            compCar = "mx5";

        if (userCar.getHorsepower() > compCar.getHorsepower())
        {
            System.out.println("You won!");
        } else { System.out.println("That's embarrassing."); }

    }

}

您正试图在 Car class 中对 java.lang.String 的实例调用方法。您的 userCar 变量是一个字符串。它应该是 CarCar subclass.

没有办法使用字符串作为变量名,句号。

但你可以这样做:

    Car userCar = null;

    if(userChoice == 1)
        userCar = supra;
    else if(userChoice == 2)
        userCar = rx7;
    else if(userChoice == 3)
        userCar = s2000;
    else if(userChoice == 4)
        userCar = mx5;

compCar.

也类似