Java 在扩展其他类的类中设置变量
我想做的事的借口是Java 在扩展其他类的类中设置变量,java,arrays,super,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].ge
-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(“第一个是%d\n”,arrayList.getFirst())时,从main开始代码>我将值和姓氏的值设为null,那么,如何正确地为数组中的对象设置值
编辑类中由初始化的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;
public Table[]创建表数组(表表,整数){
表[]表列表=新表[编号];
int i=0;
对于(i=0;i
您的构造函数不保存firstName,它应该如下所示:
Employee(String first, String last) {
super(last);
firstName = first;
}
您并没有成为Person类的良好构造函数,并且它类并没有实例变量lastName,您应该在该变量中指定在构造函数中获得的值作为参数
此外,Employee的构造函数不为firstName分配任何值
什么ArrayList?正如我看到的,您正在使用数组?我在任何地方的代码中都没有看到它?
System.out.printf(“第一个是%d\n”,**arrayList**.getFirst());
so命令错误
任何对我有意义并且可以编译的代码都是修复这些问题并删除您放入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());
}
}
你确定,是java吗?Class
应该是Class
和setFirstLast
不返回任何内容,我们需要知道employeeList
是如何初始化的。是的,当我在netbeans ide下运行它时,第一个和最后一个名称的值都为null。好的,假设您对它进行了修剪,以使问题可读,但您的代码不能编译。正如maszter所说,您缺少将fistName
与上面列出的onesokay一起赋值,并且姓氏是如何初始化的,因为它是另一个类的一部分?在您的Person
类中,使您的构造函数类似于public Person(String last){lastName=last;}
谢谢,既然姓氏是另一个类的一部分,您如何设置它呢?Person(String last){lastName=last;}