您的构造函数不保存firstName,它应该如下所示:
Employee(String first, String last) {
super(last);
firstName = first;
}
你没有成为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());
}
}