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

Java 根据用户输入的数据创建对象,java,Java,我正在做一个家庭作业项目,要求我根据用户输入的数据创建一个对象。我有一个名为Person的类,它获取基本信息,一个名为Customer的类扩展Person类并包含一个Customer编号,还有一个名为Employee的类扩展Person类并返回一个社会保险号 我已经从下面的主程序粘贴了代码。有几件事我有点困惑。首先,当我收集名字、姓氏等信息时,我应该以某种方式访问我的Person类吗 其次,我想更清楚地说,我如何创建对象?到目前为止,在我在网上读到的所有例子中,我发现他们似乎已经输入了信息,就像

我正在做一个家庭作业项目,要求我根据用户输入的数据创建一个对象。我有一个名为Person的类,它获取基本信息,一个名为Customer的类扩展Person类并包含一个Customer编号,还有一个名为Employee的类扩展Person类并返回一个社会保险号

我已经从下面的主程序粘贴了代码。有几件事我有点困惑。首先,当我收集名字、姓氏等信息时,我应该以某种方式访问我的Person类吗

其次,我想更清楚地说,我如何创建对象?到目前为止,在我在网上读到的所有例子中,我发现他们似乎已经输入了信息,就像我要说的那样

    Person firstName = new Person(Jack);
虽然我是从用户那里收集信息的,所以我不知道该怎么说

    Person firstName = new Person (enter info from user here);
最后,这是一个非常愚蠢的问题,但我必须创建一个接受Person对象的静态方法。 要创建静态方法,我假设它是

    Public Static print()
但是我如何告诉它打印person类中的内容呢?它怎么知道

我在书中的大多数示例都包含一个类,该类包含所有信息,而不是让用户输入,这很令人困惑,因为现在我被告知用户可以自由键入他们想要的内容,我需要收集这些信息

    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);

以下是这两种方法的片段:

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.
}

首先,我观察到没有Person对象。我想你会去创造它,所以我不会太在意它

就实际获取数据而言,你已经成功了一半。根据您想要框显Person对象的方式,您可以通过传递从用户收到的值来创建新的Customer或Employee对象

Customer customer = new Customer(firstName, lastName, email, custNumber);

以下是这两种方法的片段:

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.
}

要打印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;
    }
}

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

要打印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;
    }
}

要创建对象,您可以从命令行读取字符串,然后按照Makoto的建议将其传递到构造函数。

谢谢您的帮助。我还得再读几遍,但我很感谢你的帮助。我在Employee和Customer类中有一个覆盖的toString,其中包括cusNumber和empSoc。我是否需要做一些与您发布的内容不同的事情?toString方法将使打印语句更容易,而无需使用访问器字段。这是打印有关对象信息的更优选方式;除非你的作业要求你使用静态void方法,否则我会选择toString。我想我理解你现在说的。引用她对我说的任务
将对象的数据打印到控制台,此应用程序应使用名为print的静态方法来接受Person对象。感谢您的帮助。我还得再读几遍,但我很感谢你的帮助。我在Employee和Customer类中有一个覆盖的toString,其中包括cusNumber和empSoc。我是否需要做一些与您发布的内容不同的事情?toString方法将使打印语句更容易,而无需使用访问器字段。这是打印有关对象信息的更优选方式;除非你的作业要求你使用静态void方法,否则我会选择toString。我想我理解你现在说的。引用她所说的将对象的数据打印到控制台的分配,这个应用程序应该使用一个名为print的静态方法,该方法接受Person对象。