简体   繁体   English

无法从 Java 中的另一个类调用方法

[英]Can't call method from another class in Java

I'm new to Java and honestly its OOP focus is quite taxing for me at the moment.我是 Java 的新手,老实说,目前它对 OOP 的关注对我来说相当繁重。

For an Uni project where we're meant to practice this focus, I'm tasked with creating at least 2 classes: One class should be for an airline customer and the other class should contain methods to register their purchase.对于我们打算练习这一重点的 Uni 项目,我的任务是创建至少 2 个类:一个类应该用于航空公司客户,另一个类应该包含注册他们购买的方法。

I have a main file, a Persona (Person) class, and a RegistroCompra (Purchase registration) class.我有一个主文件、一个 Persona(人)类和一个 RegistroCompra(购买注册)类。 Person is supposed to have all of the following attributes, which I'm handling as private variables so that every instance of Person can get one of their own. Person 应该具有以下所有属性,我将这些属性作为私有变量进行处理,以便 Person 的每个实例都可以获得它们自己的一个。 (The attributes being asked for are stuff like personal data, ticket number, seat number and such) (要求的属性是个人数据、票号、座位号等)

public class Persona {



    private String nombrePasajero;
    private String apellidoPasajero;
    private String generoPasajero;
    private String pasaportePasajero;
    private String numTiquetePasajero;
    private String numVueloPasajero;
    private String destinoPasajero;
    private String asientoString;
    private int precioBoleto;
    private int edadPasajero;
    private int numAsientoPasajero;

    //Constructor

    public Persona(String nombre, String apellido, String genero, int edad, String pasaporte) {

        nombrePasajero = nombre;
        apellidoPasajero = apellido;
        generoPasajero = genero;
        pasaportePasajero = pasaporte;
        edadPasajero = edad;

    }

    public void setDestino() {
        destinoPasajero = RegistroCompra.obtenerDestino();
    }

And my RegistroCompra class, which is meant to set the data related not to personal information but to the information of destination, flight number and such.还有我的 RegistroCompra 类,它的目的是设置与个人信息无关的数据,而是与目的地、航班号等信息相关的数据。 All of the data set in RegistroCompra has to be fetched by Persona, because only Persona will be printed in main to verify all of the information. RegistroCompra 中的所有数据集都必须由 Persona 获取,因为只有 Persona 会在 main 中打印以验证所有信息。

public class RegistroCompra {

    private String destino;

    public void seleccionarDestino() {
    Scanner input = new Scanner(System.in);
    System.out.println("Por favor digite el destino, las opciones actuales son Nicaragua o Panama\n");

    String destino = input.nextLine();
}
    public String obtenerDestino() {
        return destino;
    }

}

However, I get an error at the Persona.setDestino() method, saying "non-static method obtenerDestino cannot be referenced from astatic context"但是,我在 Persona.setDestino() 方法中收到错误消息,说“不能从静态上下文中引用非静态方法 obtenerDestino”

I don't understand why this is going on.我不明白为什么会这样。 If I try to turn RegistroCompra.obtenerDestino() into a static method, I get an error because "destino is a non static variable", but it's being defined as public in the RegistroCompra class...如果我尝试将 RegistroCompra.obtenerDestino() 转换为静态方法,我会收到一个错误,因为“destino 是一个非静态变量”,但它在 RegistroCompra 类中被定义为 public...

You have to do something like below:您必须执行以下操作:

public class Persona {
   ...
   //You should have instance of RegistroCompra
   RegistroCompra registraCompra = new RegistroCompra();
   public void setDestino() {
        //Option 1: Explicitly call the method
        registraCompra.seleccionarDestino();
        destinoPasajero = registraCompra.obtenerDestino();
    }
}

public class RegistroCompra {

    private String destino;

    public RegistroCompra(){
       //Option 2 : Call the method in constructor
       registraCompra();
    }
    public void seleccionarDestino() {
    ...
    //Set the input to the class level variable destino
    this.destino = input.nextLine();
}
    public String obtenerDestino() {
        return this.destino;
    }

}


You can do this making destino variable and obtenerDestino() method static.您可以这样做使destino变量和obtenerDestino()方法静态。 Check the below changes to RegistroCompra class:检查以下对RegistroCompra类的更改:

public class RegistroCompra {
    private static String destino;

    public void seleccionarDestino() {
        Scanner input = new Scanner(System.in);
        System.out.println("Por favor digite el destino, las opciones actuales son Nicaragua o Panama\n");

        String destino = input.nextLine();
    }

    public static String obtenerDestino() {
        return destino;
    }
}

You are calling an instance method without having an instance.您在没有实例的情况下调用实例方法。 You have to instantiate the class (create an instance from that class) before you are able to call instance methods.您必须先实例化该类(从该类创建一个实例),然后才能调用实例方法。

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

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