如何引用对象的实例?

问题描述 投票:0回答:1

这是我在文件中创建一个类的新实例然后我试图在另一个文件中引用该特定实例的事情。

这是RegistroUI中对象的初始化。

CuentaBancaria Cuenta = new CuentaBancaria(nombre);

这是我的代码,试图在 IngresoUI 中引用同一个类。

textBienvenido.setText("BIENVENID@ USUARIO" + Cuenta.getNombre());

所有这些都在同一个包中,并用 java 编写。 请任何想法都会有所帮助:D

我无法创建另一个对象,因为它不会具有相同的属性值,而且我不需要导入,因为已经在同一个包中了。

java swing user-interface netbeans
1个回答
0
投票

要引用对象的实例,您需要以某种方式将其传递到 IngresoUI 中。例如通过在 IngresoUI 的构造函数中注入。

CuentaBancaria cuenta;
IngresoUI(CuentaBancaria cuenta){
    this.cuenta=cuenta
}

另一种方法是将IngresoUI注入RegistroUI,然后将CuentaBancaria cuenta传入IngresoUI的某个方法。

IngresoUI ingresoUI;
RegistroUI( IngresoUI ingresoUI){
    this.ingresoUI = ingresoUI;
}
void someMethodOfRegistroUI(){ //Method where you create CuentaBancaria 
    int nobre = 10;
    CuentaBancaria cuenta = new CuentaBancaria(nombre);
    ingresoUI.someMethodOfIngresoUI(cuenta);//call a method of IngresoUI
    //where you need CuentaBancaria cuenta;
}
© www.soinside.com 2019 - 2024. All rights reserved.