简体   繁体   English

数组在一个类中初始化,但内容在另一类中不可访问

[英]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.

相关问题 一个数组的内容到另一个数组 - contents of one array to another array 另一个类中的链表可以通过一种方法访问但不能通过另一种方法访问 - A linked list in another class accessible from one method but not from another 如何将在一个方法中初始化的变量用于另一类中的另一方法 - How to use variables initialized in one method into another method in another class 在完全初始化(由另一个线程)之前使用的类(在一个线程中)? - Class used (in one thread) before being fully initialized (by another)? 如何从另一个方法访问在一个方法中初始化的String数组? - How to access a String array initialized in one method from another method? 在Java中将表的内容从一个类保留到另一个类 - Keeping the contents of tables from one class to another class in java 如何修改对 class 的访问,以便一个特定属性只能由另一个 class 的成员访问? - How to modify access to a class so that one particular attribute is only accessible by members of another class? 私有类数组的长度无法访问 - length of array of private class is not accessible 如何搜索内容与另一内容匹配的一个数组? - How do I search for one array whose contents match another? 将数组内容分配给另一个 - Assigning Array Contents to Another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM