在类中设置变量以扩展其他类

时间:2014-09-23 21:20:05

标签: java arrays super

我试图做的事情是

-send Array of EMPLOYEE objects to Restaurant Class
-In Class RESTAURANT give each of the employee objects a name and last name (last name not in employee Class but in PERSON Class which Employee CLass Extends.
-print say employeeList[1].getLastName()

希望我的代码更好地解释

class Person {
    public Person(final String last) {

    }
}

class Employee extends Person {
    private String firstName;

    // getFirstName method
    // getLastName Method

    Employee(final String first, final String last) {
        super(last);
    }
}

class Restaurant { // set first object in array have first and last name

    public void setFirstLast(final Employee[] employeeList) {
        String firstName = "Jovana";
        String lastName = "Valdez";
        employeeList[0] = new Employee(firstName, lastName); // set up via constructor
    }
}

public class Main {

    private String lastName;

    public static void main(final String[] args) {
        Employee[] employeeList = new Employee[1]; // my array of Employee objects, all set to null

        Restaurant restaurant = new Restaurant();
        restaurant.setFirstLast(employeeList); 

    }

}
当我尝试打印System.out.printf("first is %d\n",arrayList.getFirst());时从

我得到值为null以及姓氏的值,那么正确的方法是什么,并为数组中的对象设置值?

通过

编辑在Class restaurant中初始化的arrayList
public Table[] create_table_array(Table table,int number) {
    Table[] TableList = new Table[number];
    int i = 0;
    for(i = 0; i < number; i++) {
        TableList[i] = table;
    }
    return TableList;

2 个答案:

答案 0 :(得分:0)

您的构造函数不保存firstName,它应该如下所示:

Employee(String first, String last) {
    super(last);
    firstName = first;
}   

答案 1 :(得分:0)

你没有成为Person类的好构造函数,并且它没有实例变量lastName,你应该将构造函数中的值作为参数赋值。

Employee的构造函数也没有为firstName赋值。

什么是ArrayList?正如我看到你正在使用数组?我在代码中没有看到它? System.out.printf("first is %d\n",**arrayList**.getFirst());所以命令错了。

任何对我有意义且可以编译的代码都是修复这些内容并删除您在System.out.printf中放置的格式选项,因为您没有格式化数字。

所以代码看起来像:

    class Person {
    String lastName;

    public Person(final String last) {
        lastName=last;
    }
}

    class Employee extends Person {
        private String firstName;

        public String getFirstName()
        {return firstName;}

        public String getLastName()
        {return lastName;}

        Employee(final String first, final String last) {
            super(last);
            firstName=first;
        }
    }
    class Restaurant { // set first object in array have first and last name

    public void setFirstLast(final Employee[] employeeList) {
        String firstName = "Jovana";
        String lastName = "Valdez";
        employeeList[0] = new Employee(firstName, lastName); // set up via constructor
    }
}

    public class Main {

    private String lastName;

    public static void main(final String[] args) {
        Employee[] employeeList = new Employee[1]; 

        Restaurant restaurant = new Restaurant();
        restaurant.setFirstLast(employeeList); 
        System.out.printf("first is "+employeeList[0].getFirstName()+"  "+employeeList[0].getLastName());

    }

}