从用户输入的数据创建对象
我正在做一个家庭作业项目,需要我从用户输入的数据创建一个对象。我有一个名为Person的类,它接受基本信息,一个名为Customer的类,它扩展了Person类,并包含一个客户号和一个名为Employee的类,它扩展Person类并返回一个社会安全号。从用户输入的数据创建对象
我粘贴了下面我主程序的代码。我对一些事情有点困惑。首先,当我收集信息(名字,姓氏等),我应该在那里访问我的Person类吗?
第二我猜得更清楚,我该如何创建对象?在所有的我都在线阅读,我觉得到目前为止的例子,他们似乎已经进入信息一样,如果我是把它说
Person firstName = new Person(Jack);
虽然我收集来自用户的信息,所以我不看如何分辨它像
Person firstName = new Person (enter info from user here);
最后又一次,这是一个非常愚蠢的问题,但我必须创建一个接受Person对象的静态方法。 要创建静态方法我假设它是
Public Static print()
,但我怎么告诉它打印从人类的东西吗?它是如何知道的?
本书中的大多数示例都包含一个包含所有信息的类,而不是让用户输入它,这让人感到困惑,因为现在我被告知用户可以自由输入他们想要的内容,而且我需要收集这些信息。
import java.util.Scanner;
public class PersonApp
{
public static void main(String[] args)
{
//welcome user to person tester
System.out.println("Welcome to the Person Tester Application");
System.out.println();
Scanner in = new Scanner(System.in);
//set choice to y
String choice = "y";
while (choice.equalsIgnoreCase("y"))
{
//prompt user to enter customer or employee
System.out.println("Create customer or employee (c/e): ");
String input = in.nextLine();
if (input.equalsIgnoreCase("c"))
{
String firstName = Validator.getString(in, "Enter first name: ");
String lastName = Validator.getString(in, "Enter last name: ");
String email = Validator.getEmail(in, "Enter email address: ");
String custNumber = Validator.getString(in, "Customer number: ");
}
else if(input.equalsIgnoreCase("e"))
{
String firstName = Validator.getString(in, "Enter first name: ");
String lastName = Validator.getString(in, "Enter last name: ");
String email = Validator.getEmail(in, "Enter email address: ");
int empSoc = Validator.getInt(in, "Social security number: ");
}
}
System.out.println("Continue? y/n: ");
choice = in.next();
}
}
首先,我注意到,没有一个Person
对象。我假设你会开始创造它,所以我不会过多地关心自己。
就实际获取数据而言,您已经达到了一半。根据您要如何构建对象Person
,您可以通过传递您从用户处收到的值来创建新对象Customer
或Employee
。
Customer customer = new Customer(firstName, lastName, email, custNumber);
或
Employee employee = new Employee(firstName, lastName, email, empSoc);
下面是两者的片段:
public class Person {
public Person (String first, String last, String email) {
// You'd fill in code here for handling the variables
}
// ...
}
public class Customer extends Person {
public Customer (String first, String last, String email, String custNo) {
super(first, last, email);
// You'd fill in code here for handling the variables
}
// ...
}
public class Employee extends Person {
public Employee (int social) {
super(first, last, email);
// You'd fill in code here for handling the variables
}
// ...
}
要从Person
类打印的东西,使用静态方法(为什么你可以重写toString()
?相反),您将其设置为使您的Person
对象具有accesso rs与每个与Person
相关的字段。这意味着如果您是员工或客户,您将拥有与该对象相关的getFirstName()
,getLastName()
等等。 (我把它作为一个练习给你。)
从这个意义上说,那么只需要调用这些访问器来打印值。
public static void print(Person p) {
System.out.println(p.getFirstName()) + " " + p.getLastName()); // You can get the trend from here.
}
要打印Person对象,你可以只使用System.out.println()
如果你只是想将它打印到命令行,但你会得到一些不可读的废话。
println()方法所做的是,如果对象不是字符串调用,则它的方法是toString()
,因为所有对象都有一个,所以它在java.lang.Object
中定义。但是,这种方法给了我们上面提到的不可读的东西,所以你必须重写它像做
public class Person
{
String firstName;
String Lastname;
public Person(String firstName, String lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
public String toString()
{
// Create a String that represents this object
return "This person is called " + firstName + " " + lastName;
}
}
要创建一个对象,你可以在命令行读取的字符串,然后将它们传递到构造函数中诚建议。
谢谢你的帮助。我将不得不再次阅读它,但是我很感谢帮助。我在Employee和Customer类中有一个重载toString(),它将包含cusNumber和empSoc。我需要做一些与你发布的不一样的东西吗? – 2012-03-15 14:33:41
'toString()'方法将使打印语句更容易,而无需使用访问器字段。这是打印关于Object的信息的更好的方法。除非你的任务需要使用static void方法,否则我会选择'toString()'。 – Makoto 2012-03-15 14:39:01
我想我明白你现在在说什么。引用她所说的任务:“要将对象的数据打印到控制台,此应用程序应该使用名为print的静态方法来接受Person对象。” – 2012-03-15 15:02:27