根据用户输入的数据创建对象

时间:2012-03-15 14:19:28

标签: java

我正在做一个家庭作业项目,要求我根据用户输入的数据创建一个对象。我有一个名为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();


}

}

2 个答案:

答案 0 :(得分:0)

首先,我发现没有Person个对象。我假设你会开始创造它,所以我不会过多地关注它。

在实际获取数据的情况下,你已经到了一半。根据您想要构建Person对象的方式,您可以通过传递从用户收到的值来创建新的CustomerEmployee对象。

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对象具有每个属性的访问权限与Person相关的字段。这意味着您有一个getFirstName()getLastName()等等,如果它是一个员工或一个客户,则与该对象相关。 (我把它作为练习留给你。)

从这个意义上说,人们只需要调用这些访问者来打印值。

public static void print(Person p) {

    System.out.println(p.getFirstName()) + " " + p.getLastName()); // You can get the trend from here.
}

答案 1 :(得分:0)

要打印Person对象,如果只想将其打印到命令行,可以使用System.out.println(),但是你会得到一些不可读的废话。
println()方法的作用是,如果对象不是String,则调用它的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;
    }
}

要创建一个对象,您可以从命令行读取字符串,然后将其传递给构造函数,如Makoto建议的那样。

相关问题