调用 class 的实例化版本 我在另一个 class 的按钮内创建

Calling an instantiated version of a class I made within a button in another class

基本上我想做的是从 InitialScreen class 中的 nextStageButton 引用 myHashTable,在 ArrayAddingScreen class 中的 addingButton 中。我在我的代码中加入了注释,试图更好地解释我自己。我真的很想完成这项任务,所以如果您发现我做错了什么或者我可以做得更好,请提供给我这种见解,我们将不胜感激。如果有人感兴趣的话,任务是制作一个哈希表并在上面存储客户

编辑:为了相关性删减了一些内容

UI 屏幕 1

package hashtableinsert;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 *
 * @author Administrator
 */
public class InitialScreen extends Application {
//code went here but it wasn't relevant so I took it out
        nextStageButton.setOnAction(new EventHandler<ActionEvent>(){

            @Override
            public void handle(ActionEvent t)
            {
                if(arraySize.getText().equals("")){
                    errorLabel.setVisible(true);
                }else{
                int passingToN = Integer.parseInt(arraySize.getText());
//I need to reference this in the adding button
                Hashtable myHashTable = new Hashtable(passingToN);
                errorLabel.setVisible(false);
                primaryStage.hide();
                arraySize.clear();

                nextStage = new ArrayAddingScreen(firstStage);

                }
            }               
            });         
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

UI 屏幕 2

package hashtableinsert;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 *
 * @author Administrator
 */
public class ArrayAddingScreen extends Application {
   //Same as before trimmed for relevance
        addingButton.setOnAction(new EventHandler<ActionEvent>(){
            @Override
            public void handle(ActionEvent t)
            {
               String nameString = nameInput.getText();
               String cityString = cityInput.getText();               
               Client myClient = new Client(nameString,cityString);
               //I need to access myHashTable here

            }

        });
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

客户class

package hashtableinsert;
/**
 * Write a description of class Client here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Client
{
    private String name;
    private String city;

    public Client(String name, String city)
    {
        this.name = name;
        this.city = city;
    }

    public String toString()
    {
        return name + " " + city;
    }

    public String getName()
    {
        return name;
    }

    public String getCity()
    {
        return city;
    }
}

哈希表class

包哈希表插入; /** * 在这里写下 class HashTable 的描述。 * * @author(你的名字) * @version(版本号或日期) */ public class 哈希表 {

    private int n;
    private Client[] table;

    public Hashtable(int n) {
        this.n = n;
        table = new Client[n];
    }

    public int hashFunction(String key) {
        int sum = 0;
        for (int i = 0; i < key.length(); i++) {
            sum += (int) key.charAt(i);
        }
        return sum;
    }

    public String search(String key) {       
        int sum = 0;
        for (int i = 0; i < key.length(); i++) {
            sum += (int) key.charAt(i);
        }

        if (key.equals(table[sum])) {
            return table[sum].toString();
        } else if (table[sum] == null) {
            return null;
        } else {
            while (table[sum] != null) {
                sum++;
            }
        }
           return table[sum].toString();
    }

    public boolean insert(Client myClient) {
        int counter = 0;
        String temp = myClient.getName();
        boolean ret = false;
        int tempSum = 0;
        for (int i = 0; i < temp.length(); i++) {
            tempSum += (int) temp.charAt(i);
        }

        if (table[tempSum] == null) {
            table[tempSum] = myClient;
            ret = true;
        } else {
            while (table[tempSum] != null) {
                if(tempSum == table.length){
                    tempSum = -1;
                }
                tempSum++;
                counter++;
            }
            if(counter != n){
             ret = true;  
             table[tempSum] = myClient;
            }
        } 
        return ret;
    }
}

如果您需要在新实例化的 class 中访问某些内容,则可以在其构造函数中或作为 setter 方法将其传递给 class

Hashtable myHashTable = new Hashtable(passingToN);
.....
// ArrayAddingScreen needs to read/write myHashTable so we will pass it
nextStage = new ArrayAddingScreen(firstStage, myHashTable);

// OR
// create a setter method in ArrayAddingScreen class

public void setMyhashTable (Hashtable myHashTable) {
   this.myHashTable = myHashTable;
}