【问题标题】:Can't call method from another class in Java无法从Java中的另一个类调用方法
【发布时间】:2020-05-27 13:21:17
【问题描述】:

我是 Java 新手,老实说,它的 OOP 重点目前对我来说相当费力。

对于一个我们旨在实践这一重点的 Uni 项目,我的任务是创建至少 2 个类: 一个类应该是针对航空公司客户的,另一个类应该包含注册他们购买的方法。

我有一个主文件、一个 Persona(人)类和一个 RegistroCompra(购买注册)类。 Person 应该具有以下所有属性,我将其作为私有变量处理,以便 Person 的每个实例都可以获得自己的一个。 (要求的属性是个人数据、票号、座位号等)

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();
    }

还有我的 RegistroCompra 类,它的目的是设置与个人信息相关的数据,而不是与目的地、航班号等信息相关的数据。 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;
    }

}

但是,我在 Persona.setDestino() 方法中遇到错误,提示“无法从静态上下文引用非静态方法 obtenerDestino”

我不明白为什么会这样。如果我尝试将 RegistroCompra.obtenerDestino() 转换为静态方法,我会收到一个错误,因为“destino 是一个非静态变量”,但它在 RegistroCompra 类中被定义为公共...

【问题讨论】:

    标签: java class methods static non-static


    【解决方案1】:

    您必须执行以下操作:

    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;
        }
    
    }
    
    
    

    【讨论】:

    • 嘿,如果RegistroCompra没有构造函数有问题吗?此时,RegistroCompra 的实例包含哪些数据?你为什么要使用这个。而不是 RegistroCompra?我知道这是对类的引用,但为什么不使用类名呢?
    • 您需要通过构造函数调用方法 seleccionarDestino() 或显式调用该方法以获取输入。这是您的设计决定。
    • RegistroCompra 是类的名称。 this 指的是类的当前实例(对象)。 this.destino 澄清了您的意思是成员变量。 destino = 也可以,但不是 String destino = 这是它自己的新变量。
    【解决方案2】:

    您可以将destino 变量和obtenerDestino() 方法设为静态。检查以下对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;
        }
    }
    

    【讨论】:

      【解决方案3】:

      您在没有实例的情况下调用实例方法。您必须先实例化该类(从该类创建一个实例),然后才能调用实例方法。

      【讨论】:

      • 既然我得到了那部分,为什么不让我把 obtenerDestino 变成一个静态类?将 RegistroCompra 转换为静态类会有什么缺点吗?它的工作只是设置一些值并返回它们,以便可以将它们分配给 Persona 的属性
      • 因为obtenerDestino 正在访问一个实例变量(destino)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-30
      • 1970-01-01
      • 2015-08-17
      • 2016-01-08
      相关资源
      最近更新 更多