Java 在类之间传递带有getter和setter的userInput
我的getter返回null。我试图收集用户输入并将其发送到另一个类,通过使用getter和setter来显示。在ClientInput类中,我收集用户输入,并使用setter将客户机类中用户输入的值分配给实例变量。然后,我希望从ClientCalculations类中访问实例变量的值。当我尝试创建一个新实例时,它会给我一个空值。如何从另一个类中获取使用userinput设置的值?希望我能理解我是java新手Java 在类之间传递带有getter和setter的userInput,java,Java,我的getter返回null。我试图收集用户输入并将其发送到另一个类,通过使用getter和setter来显示。在ClientInput类中,我收集用户输入,并使用setter将客户机类中用户输入的值分配给实例变量。然后,我希望从ClientCalculations类中访问实例变量的值。当我尝试创建一个新实例时,它会给我一个空值。如何从另一个类中获取使用userinput设置的值?希望我能理解我是java新手 public class Client{ //Instance variabl
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());
}
}
公共类客户端{
//客户端信息的实例变量
私有字符串ClientName;
私有字符串ClientLawnWidth;
私有字符串ClientLawnLength;
私有字符串ClientPaymentMethod;
私有字符串客户端问题;
/*
*客户端名称的Getter和setter
*
*
*/
public void setName(字符串ClientName){
this.ClientName=ClientName;
}
公共字符串getName(){
返回ClientName;
}
/*
*客户端名称的Getter和setter
*
*
*/
public void setClientLawnWidth(字符串ClientLawnWidth){
this.ClientLawnWidth=ClientLawnWidth;
}
公共字符串getClientLawnWidth(){
返回ClientLawnWidth;
}
/*
*客户端名称的Getter和setter
*
*
*/
public void setClientLawnLength(字符串ClientLawnLength){
this.ClientLawnLength=ClientLawnLength;
}
公共字符串getClientLawnLength(){
returnclientlawnlength;
}
/*
*客户支付方法的Getter和setter
*
*
*/
public void setClientPaymentMethod(字符串ClientPaymentMethod){
this.ClientPaymentMethod=ClientPaymentMethod;
}
公共字符串getClientPaymentMethod(){
returnclientpaymentmethod;
}
/*
*客户问题的Getter和setter
*
*
*/
public void setClientQuestion(字符串ClientQuestion){
this.ClientQuestion=ClientQuestion;
}
公共字符串getClientQuestion(){
返回客户问题;
}
公共字符串显示(){
返回“客户端名称:”+getName()
+“\n客户端草坪宽度:”+getClientLawnWidth()
+“\n客户端付款方式:”+getClientPaymentMethod();
}
}
公共类客户端输入{
私人ArrayList客户;
私有InputHelper输入;
公开募捐{
clientDataEntry();
显示客户端();
}
public void clientDataEntry(){
字符串ClientName=“”;
字符串ClientLawnWidth=“”;
字符串ClientLawnLength=“”;
字符串ClientPaymentMethod=“”;
字符串ClientQuestion=“”;
字符串more=“”;
Client newClient=null;
Clients=newarraylist();
while(true){
输入=新的InputHelper();
ClientName=input.getUserInput(
“输入客户名称”);
ClientLawnWidth=input.getUserInput(
“输入客户草坪的宽度(以码为单位”);
ClientLawnLength=input.getUserInput(
“输入客户草坪的长度(以码为单位”);
ClientPaymentMethod=input.getUserInput(
“输入您的付款方式,1、2或22次付款”);
newClient=newClient();
newClient.setName(ClientName);
setClientLawnWidth(ClientLawnWidth);
newClient.setClientLawnLength(ClientLawnLength);
setClientPaymentMethod(ClientPaymentMethod);
Clients.add(newClient);
more=input.getUserInput(
“您想再输入一个吗?”);
如果(!more.equals(“y”)){
打破
}
}
}
公共客户机(){
Client=null;
对于(int i=0;i
在哪里初始化即时变量?
在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());
}
}
错误发生在哪里?你在用如此复杂的方式做简单的事情。。。