简体   繁体   English

无法在类构造函数中存储数据,学习OOP

[英]Having trouble storing data in class constructors, learning OOP

Here is the assignment I"m supposed to complete: 这是我应该完成的作业:

Write a program that models an employee. 编写一个模拟员工的程序。 An employee has an employee number, a name, an address, and a hire date. 员工有员工编号,姓名,地址和雇用日期。 A name consists of a first name and a last name. 名称由名字和姓氏组成。 An address consists of a street, a city, a state (2 characters), and a 5-digit zip code. 地址由街道,城市,州(2个字符)和5位邮政编码组成。 A date consists of an integer month, day and year. 日期由整数,月,日和年组成。

Use an Employee class, a Name class, an Address class, and a Date class in your solution. 在解决方案中使用Employee类,Name类,Address类和Date类。

Your program should prompt the user to enter data for several employees and then display that data. 您的程序应提示用户输入多个员工的数据,然后显示该数据。 The number of employees to store data for shall be entered from the command line. 应从命令行输入要存储数据的员工数。

What I'm confused about is how to use all the different classes for storing info. 我感到困惑的是如何使用所有不同的类来存储信息。

Here is my code (sorry this post is so dang long) 这是我的代码(对不起,这篇文章太长了)

    import java.util.Scanner;

    public class unitTenDemo
    {
public static void main ( String [ ] args )
{
    Scanner input = new Scanner ( System.in );
    System.out.print ( "Enter the number of employees" );
    System.out.println ( "\t" );
    int employees = input.nextInt ( );

    for (  int count = 0; count < employees; count ++ )
    {
        System.out.print ( "Enter the employees' numbers" );

        int employeeNumber = input.nextInt ( );
        System.out.println ( );
        System.out.println ( "The number is " +employeeNumber );    
        System.out.println ( );
    }
        }
    }

//that was the actual output code //这是实际的输出代码

//here's the constructor that I'm stuck on //这是我坚持的构造函数

    public class unitTen
{
int employeeNumber;

public int Employee ( int empNum )
{
    employeeNumber = empNum;
}

string employeeName;

public void Name ( string empName )
{
    employeeName = empName;
}

string street;
string city;
string state;
int zipCode;



}

Don't put everything into the constructor. 不要将所有内容放入构造函数中。 It's okay to write a constructor that builds an object that is not fully initialized. 可以编写一个构造未完全初始化的对象的构造函数。 You can organize your program as follows: 您可以按以下方式组织程序:

  1. Find out how many Employee objects there will be (user input) 找出将有多少个Employee对象(用户输入)
  2. Create an array of Employee objects of the appropriate length 创建适当长度的Employee对象数组
  3. For each element of the array, assign a new Employee to that element 对于数组的每个元素,为该元素分配一个新的Employee
  4. For each element of the array, prompt the user for each piece of data needed to properly initialize the Employee . 对于数组的每个元素,提示用户输入正确初始化Employee所需的每条数据。

The last step (which deals with only one Employee at a time) will break down into a lot of details, since each Employee object has a lot of information. 最后一步(一次只处理一个Employee )将分解成很多细节,因为每个Employee对象都有很多信息。 Just go through all the elements systematically. 只需系统地浏览所有元素。

This code won't compile at all. 这段代码根本不会编译。 Yopu have declaired int as return type and not returning anything from the method. Yopu将int声明为返回类型,并且未从该方法返回任何内容。

 public int Employee ( int empNum )
 {
      employeeNumber = empNum;
 }

In addition to the answer pointed to by @Ted , you should modify your Employee class accordingly and then invoke the constructors as you please. 除了@Ted指出的答案外,您还应该相应地修改Employee类,然后根据需要调用构造函数。

public class Employee // you should change the name of class to Employee
{
int employeeNumber;

public Employee(){}; // default constructor to create empty Employee objects
public Employee ( int empNum ) // constructor cannot have any return type
{
    employeeNumber = empNum;
}

string employeeName;

public Employee( string empName, int empNum ) // you can create multiple constructors with different parameters.
{
    employeeName = empName;
    employeeNumber = empNum;
}

string street;
string city;
string state;
int zipCode;

// you can create getters and setters for these fields

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM