首页 > 解决方案 > 在类之间使用 getter 和 setter 传递 userInput

问题描述

我的吸气剂返回 null。我正在尝试收集用户输入并将其发送到另一个类,以便通过使用 getter 和 setter 来显示。在 ClientInput 类中,我正在收集用户输入,并使用 setter 为实例变量分配 Client 类中用户输入的值。然后从那里我希望从 ClientCalculations 类中访问实例变量的值。当我尝试它时,它给了我一个空值,因为我正在创建一个新实例。如何从另一个类中获取我使用用户输入设置的值?希望我是有道理的,我是 Java 新手。

public class Client{
    //Instance variables for client information
    private String ClientName;
    private String ClientLawnWidth;
    private String ClientLawnLength;
    private String ClientPaymentMethod;
    private String ClientQuestion;

    /*
    *Getter and setter for Client name
    *
    *
    */

    public void setName(String ClientName) {
        this.ClientName = ClientName;
    }
    public String getName() {
        return ClientName;
    }

    /*
    *Getter and setter for Client name
    *
    *
    */

    public void setClientLawnWidth(String ClientLawnWidth) {
        this.ClientLawnWidth = ClientLawnWidth;
    }
    public String getClientLawnWidth() {

        return ClientLawnWidth;
    }
    /*
    *Getter and setter for Client name
    *
    *
    */

    public void setClientLawnLength(String ClientLawnLength) {
        this.ClientLawnLength = ClientLawnLength;
    }

    public String getClientLawnLength() {
        return ClientLawnLength;

    }

    /*
    *Getter and setter for Client Payment Method
    *
    *
    */

    public void setClientPaymentMethod(String ClientPaymentMethod) {
        this.ClientPaymentMethod = ClientPaymentMethod;
    }

    public String getClientPaymentMethod() {
        return ClientPaymentMethod;
    }       

    /*
    *Getter and setter for Client Question
    *
    *
    */

    public void setClientQuestion(String ClientQuestion) {
        this.ClientQuestion = ClientQuestion;
    }

    public String getClientQuestion() {
        return ClientQuestion;
    }       

    public String display() {
        return "Client Name: " + getName()
                + "\n Client Lawn Width: " + getClientLawnWidth()
                + "\n Client Payment Method: " + getClientPaymentMethod();
    }
}

public class ClientInput {
    private  ArrayList<Client>    Clients;
    private  InputHelper  input;

    public void run() {
    clientDataEntry();
    displayClients();
    }

    public void clientDataEntry() { 
    String ClientName = ""; 
    String ClientLawnWidth = "";    
    String ClientLawnLength = "";   
    String ClientPaymentMethod = "";    
    String ClientQuestion = ""; 
    String  more          = "";
    Client     newClient        = null;
    Clients = new ArrayList();

    while (true) {
            input = new InputHelper();
            ClientName = input.getUserInput(
                    "Enter the name of The Client");
            ClientLawnWidth = input.getUserInput(
                    "Enter the width of clients lawn in yards");
            ClientLawnLength = input.getUserInput(
                    "Enter the length of clients lawn in yards");
            ClientPaymentMethod = input.getUserInput(
                    "Enter your payment method, 1,2,or 22 payments");
            newClient = new Client();
            newClient.setName(ClientName);
            newClient.setClientLawnWidth(ClientLawnWidth);
            newClient.setClientLawnLength(ClientLawnLength);
            newClient.setClientPaymentMethod(ClientPaymentMethod);
            Clients.add(newClient);
            more = input.getUserInput(
                    "Would you like to enter another?");
            if (!more.equals("y")) {
                break;
            }
        }
    }
        public void displayClients() {
        Client  client  = null;
        for (int i = 0; i < Clients.size(); i++) {
            client = (Client) Clients.get(i);
            System.out.println(client.display());
            System.out.println();
        }
    }
 }

public class ClientCalculations {
    private String name; 
    private int servicChargeFee;
    public void display() {
    Client client = new Client();
    System.out.print(client.getName());
    }
}

标签: java

解决方案


你在哪里初始化即时变量?您可以在构造函数的帮助下初始化实例变量,之后您的 getter 将开始工作。例子:

 class Client {
        private String name;

        // constructor 
        Client(String name) {
            this.name = name;
        }

        // getter
        public String getName() {
            return this.name;
        }
        // setter
        public void setName(String name) {
            this.name = name;   
        }
}

public class Main {
        public static void main(String[] args) {
            // instance of client class
            Client client = new Client("Client Name");
            System.out.println(client.getName());               
        }
}

推荐阅读