在java中访问对象变量

时间:2014-01-12 23:52:32

标签: java

所以我正在开发一个java赋值,内容如下:

“编写一个为员工建模的程序。员工有一个员工编号,一个姓名,一个地址和一个雇用日期。一个名字由一个名字和一个姓氏组成。一个地址由一条街道组成,一个城市,州(2个字符)和5位邮政编码。日期由整数月,日和年组成。 在解决方案中使用Employee类,Name类,Address类和Date类。提供适当的类构造函数,getter方法,setter方法以及您认为必要的任何其他方法。 您的程序应提示用户输入多个员工的数据,然后显示该数据。存储数据的员工数量应从命令行输入。“

由于格式化问题,我无法发布代码,但我所做的是创建一个员工对象数组。该数组名为dbase []。然后,每个Employee对象为日期,名称和地址标题date1,name1和address1创建一个单独的对象。但是每当我尝试访问日期/名称/地址中的一个getter方法时,使用类似dbase [j] .name1.getFirstName的内容我遇到一个错误,指出找不到符号:name 1 --- location:class Employee

public class EmployeeProgram {
public static void main (String[] args) {
    int i = Input.getInt ("How many employees would you like to enter? ");
    int j;
    Employee [] dbase = new Employee [i];
    for (j = 0; j < i; j++) {
        String firstName = Input.getString ("What is an employee's first name?");
        String lastName = Input.getString ("What is this employee's last name?");

        String street = Input.getString ("On what street does this employee live?");
        String city = Input.getString ("In which city does this employee live?");
        String state = Input.getString ("In which state does this employee live? (abbreviation)");
        String zipcode = Input.getString ("What is this employee's zip code?");

        int month = Input.getInt ("In what month was this employee born?");
        int day = Input.getInt ("On what day was this employee born?");
        int year = Input.getInt ("In what year was this employee born?");

        int employeeID = Input.getInt ("What should this employee's employee id be?");

        dbase[j] = new Employee(firstName, lastName, street, city, state, zipcode, month, day, year, employeeID);
    }

    for (j = 0; j < i; j++) {
        System.out.print ( "Employee number " + (j + 1) + " is named ");
        System.out.print ( dbase[j].name.getFirst() + " " + dbase[j].name.getLast() + " and lives on " + dbase[j].name.getStreet());
        System.out.print ( " in " + dbase[j].address.getCity() + " " + dbase[j].address.getState() + ", " + dbase[j].address.getZip());
        System.out.print ( ". He will be hired on " + dbase[j].date.GetMonth() + "-" + dbase[j].date.getDay() + "-" + dbase[j].date.getYear() );
        System.out.print ( " and his ID is " + dbase[j].getEmployeeID());
        System.out.println ();
    }
}
}

class Employee {
int employeeID = 0;
Employee( String firstName1, String lastName1, String street1, String city1, String state1, String zipcode1, int month1, int day1, int year1, int employeeID1 ) {
    name name = new name( firstName1, lastName1 );
    address address = new address( street1, city1, state1, zipcode1 );
    date date = new date( month1, day1, year1);
    employeeID = employeeID1;   
}
int getEmployeeID() {
    return employeeID;
}
}

class name {
String firstName = "";
String lastName = "";
name(String newFirstName, String newLastName) {
    firstName = newFirstName;
    lastName = newLastName;
}
String getFirst() {
    return firstName;
}
String getLast() {
    return lastName;
}   
}

我没有发布地址或日期类,但它们与名称类

基本相同

2 个答案:

答案 0 :(得分:2)

您在Employee类中没有创建任何字段。只是构造函数中的局部变量。所以添加

private Name name;
private Address address;

... (类名总是UpperCase) 并为这些添加getter和setter。

在你的主要你应该使用

dbase[j].getName().getFirst()

等...

答案 1 :(得分:0)

据我所知,您的主要问题是您的Employee成员不是字段,它们是构造函数中的局部变量。这意味着它们在构造函数结束时基本消失。您应该能够移动它们,因此它们是字段,程序将编译。 Here is the relevant tutorial

class Employee {
    name name;
    address address;
    date date;
    int employeeID = 0;

    Employee( String firstName1, String lastName1, String street1, String city1, String state1, String zipcode1, int month1, int day1, int year1, int employeeID1 ) {
        name = new name( firstName1, lastName1 );
        address = new address( street1, city1, state1, zipcode1 );
        date = new date( month1, day1, year1);
        employeeID = employeeID1;   
    }
    int getEmployeeID() {
        return employeeID;
    }
}

一些小调:

  • 在Java中,the convention是所有类都以大写字母开头。 (class name应为class Name等)
  • 像您的员工这样的类应该将其字段设置为私有字段,并通过&#34; get&#34;方法。对于类似的类Bean来说,实际上是一个标准。这对初学者来说并不那么重要,因为封装是你将要学习的东西。本教程显示了一个带有#34;私有访问者&#34;的课程。方案