[英]Why is my program returning null,null,0 ,0?
我的程序应该将用户提交的信息存储到另一个类中。 然后应该执行一次小的计算以确定年龄。 最后一部分是在驱动程序中创建辅助类的实例,然后使用toString打印所有信息。 我的结果是null,null,0,0每次。 我究竟做错了什么?
public class CustomerInfoProgram {
public static Scanner scan = new Scanner(System.in);
public static String firstName;
public static String lastName;
public static String address;
public static String phoneNumber;
public static int dob;
public static int currentYear;
public static int age;
public static String name;
public static void main(String[] args) {
Customer in = new Customer();
getCustomerName();
getPhone();
getAddress();
getDOB();
getCurrentYear();
in.toString();
}
public static String getCustomerName() {
System.out.println("What is your first name?");
firstName = scan.nextLine();
System.out.println("What is your last name?");
lastName = scan.nextLine();
name = firstName + " " + lastName;
return name;
}
public static String getAddress() {
System.out.println("What is your address?");
address = scan.nextLine();
return address;
}
public static String getPhone() {
System.out.println("What is your phone number?");
phoneNumber = scan.nextLine();
return phoneNumber;
}
public static int getDOB() {
System.out.println("What year were you born?");
dob = scan.nextInt();
return dob;
}
public static int getCurrentYear() {
System.out.println("What is the current year?");
currentYear = scan.nextInt();
return currentYear;
}
}
中学班:
import java.util.Scanner;
public class Customer {
public static int age;
public static String allInfo;
public static String name = CustomerInfoProgram.name;
public static String address = CustomerInfoProgram.address;
public static String phoneNumber = CustomerInfoProgram.phoneNumber;
public static int dob = CustomerInfoProgram.dob;
public static int currentYear = CustomerInfoProgram.currentYear;
private int getAge() {
age = (currentYear - dob);
return age;
}
public String toString() {
getAge();
allInfo = (name + " " + address + " " + phoneNumber + " " + dob + " " + age);
System.out.println(allInfo);
return toString();
}
}
public static String name = CustomerInfoProgram.name;
这只会价值CustomerInfoProgram.name
当字段被初始化,即一次,当Customer
被加载的类-当它不更新CustomerInfoProgram.name
被更新。
(最晚)在调用new Customer()
时会加载Customer
类,这是在运行任何方法设置CustomerInfoProgram.name
的值之前发生的,因此当您尝试使用name
时为null。
如果要使用CustomerInfoProgram.name
的当前值,只需直接引用该字段。 另外,您可以添加一个getter,它将返回其当前值:
public static String getName() {
return CustomerInfoProgram.name;
}
然后调用getter代替name
:
allInfo = (getName() + " " + ...
(其他字段完全相同)。
您还将从toString()
方法中获得一个StackOverflowError
:
public String toString() {
getAge();
allInfo = (name + " " + address + " " + phoneNumber + " " + dob + " " + age);
System.out.println(allInfo);
return toString();
}
您将无条件地调用toString()
内的toString()
,因此这将一直进行自身调用,直到耗尽堆栈空间为止。
相反,只需返回allInfo
:
public String toString() {
getAge(); // This actually does nothing - do you intend to use it in the return value?
return (name + " " + address + " " + phoneNumber + " " + dob + " " + age);
}
并在您的主要方法中调用System.out.println
:
System.out.println(in); // Implicitly calls toString().
有关问题原因,请参见@Andy的答案。
通常的方法是在创建对象之前收集要存储在Customer
对象中的值,然后使用非默认构造函数调用。 一个部分示例可能如下所示:
// get individual values here
getCustomerName();
getPhone();
// etcetera
Customer in = new Customer(name, phone, address, dob);
剩下的我留给读者练习。
作为静态属性,两个类中的初始化都将只有一次。 因此,从用户那里获得输入后,这些值未设置为属性。 因此为空。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.