[英]array initialized in one class but contents not accessible in another
i have a javafx application and it has two classes in it, namely "Client" and "Interface_Client_Impl". 我有一个javafx应用程序,它具有两个类,即“客户端”和“接口_客户端_Impl”。 i have defined an int array in "Client" class and a function that initializes that array.
我在“ Client”类中定义了一个int数组,并定义了一个初始化该数组的函数。 when i tried to access the array contents at index i from Interface_Client_Impl, it always returns 0. Interface_Client_Impl class is accessed remotely and im able to get the values of variables but not the array.
当我尝试从Interface_Client_Impl访问索引i处的数组内容时,它始终返回0。Interface_Client_Impl类可以远程访问,无法获取变量的值,但不能获取数组的值。 where am i going wrong.
我要去哪里错了。 -_- this is what i have.
-_-这就是我所拥有的。
public class Client extends Application
{
public int size = 4;
public int array[] = new int[size];
public int min = 1;
public int max = 99;
public static void main(String[] args) throws Exception
{
launch(args);
}
@Override
public void start(Stage primaryStage) throws NotBoundException, RemoteException
{
initialize_arr();
//other codes
}
public void initialize_arr()
{
Random rand = new Random();
for(int i = 0; i < size; i++)
{//initialize with random values
int val = rand.nextInt(max - min + 1) + min;
array[i] = val;
}
}
}
//another class //另一个类
public class Interface_Client_Impl extends UnicastRemoteObject implements Interface_Client
{
public Client client = new Client();
@Override
public int exchange(int val)
{
Random rand = new Random();
int pos = rand.nextInt(client.size);
int return_val = client.array[pos];
client.array[pos] = val;
return return_val;
}
}
You have just created the instance for Client in class Interface_Client_Impl 您刚刚在Interface_Client_Impl类中为Client创建了实例。
You need to invoke the array initialization of client instance. 您需要调用客户端实例的数组初始化。 You can do via following two ways
您可以通过以下两种方式进行
By invoking public method client.start(primaryStage)
通过调用公共方法
client.start(primaryStage)
or 要么
by invoking public method client.initialize_arr()
通过调用公共方法
client.initialize_arr()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.