Java中的继承和数组
我有一个这样的班级:Java中的继承和数组,java,Java,我有一个这样的班级: public class People { private String Name; private String Address; public People(String aName, String aAddress) { this.Name=aName; this.Address=aAddress; } public String getName() { return Name;
public class People {
private String Name;
private String Address;
public People(String aName, String aAddress) {
this.Name=aName;
this.Address=aAddress;
}
public String getName() {
return Name;
}
public String getAddress() {
return Address;
}
void display() {
System.out.println("Name:\t"+Name);
System.out.println("Address:\t" +Address);
}
}
另外2个:
class Students extends People{
private int MatriculationNumber;
private String CourseName;
public Students (String aName, String aAddress, int matriculationNumber, String courseName){
super(aName,aAddress);
this.MatriculationNumber=matriculationNumber;
this.CourseName=courseName;
}
void display() {
super.display();
System.out.println("Matriculation Number: \t" +MatriculationNumber);
System.out.println("Course Name: \t" +CourseName);
}
}
class Staffs extends People{
private int EmployeeNumber;
private String Department;
public Staffs (String aName, String aAddress, int employeeNumber, String department){
super(aName,aAddress);
this.EmployeeNumber=employeeNumber;
this.Department=department;
}
void display() {
super.display();
System.out.println("Employee Number: \t" +EmployeeNumber);
System.out.println("Department: \t" +Department);
}
}
问题是如何创建一个名为“School”的类,该类有一个可以同时包含学生和职员的列表,因此我可以添加一个像AddPeople()这样的方法,该方法可以在其中添加或删除学生或职员?您可以创建一个新的职员类,该类也可以从People类扩展 然后给出基本类人员作为列表的类型 输出将是
Name: Max
Address: MustermanAddress
Matriculation Number: 181242
Course Name: CS50
Name: Christian
Address: AugustAddress
Name: Max
Address: MustermanAddress
Matriculation Number: 181242
Course Name: CS50
您可以使用下面的解决方案。该解决方案不是线程安全的,但对于您的用例应该可以很好地工作
import java.util.ArrayList;
class School {
private List<People> peopleList = new ArrayList<People>();
public void addPeople(People people) {
peopleList.add(people);
}
public void removePeople(People people) {
peopleList.remove(people);
}
public static void main(String... args) {
Student student = new Student("Name", "Address", 90, "Course");
Staff staff = new Staff("Name", "Address", 1001, "Department");
School school = new School();
school.addPeople(student);
school.addPeople(staff);
school.removePeople(student);
school.removePeople(staff);
}
}
import java.util.ArrayList;
班级学校{
private List peopleList=new ArrayList();
公共部门人员(人){
peopleList.add(人);
}
公共无效删除人(人){
人物列表。删除(人物);
}
公共静态void main(字符串…参数){
学生=新生(“姓名”、“地址”、90、“课程”);
职员=新职员(“姓名”、“地址”,1001,“部门”);
学校=新学校();
学校。添加人(学生);
学校.行政人员(工作人员);
学校。搬迁人员(学生);
学校。搬迁人员(工作人员);
}
}
关于StackOverflow,要问一个好问题,你必须展示你尝试了什么以及你到底遇到了什么问题。你可以为学校创建一个静态方法addPeople
,这样就可以通过类名调用它。但我认为将addPeople
定义为只能从学校实例调用的实例方法可能更为合理。在内部创建两个集合,一个集合包含员工,另一个集合包含学生。然后,您可以创建两个方法:addStaff和addStudent。因此,您想要一个同时包含Student和Staff的列表吗?请注意,您应该遵循Java命名约定:变量名和方法名是用camelCase编写的。ArrayList peopleContainer=new ArrayList()
better beList peopleContainer=new ArrayList()
…@GhostCat ty您为什么喜欢列表?因为您更喜欢使用接口,而不是特定的实现。事实上,使用ArrayList是一个实现细节,因此可以避免依赖它。一种方法是:给字段指定接口类型。Ty@GhostCat你是对的,我找到了一个很好的解释,是的,我想你可以找到更多关于同一主题的其他问题;-)Wew,我想我必须读更多关于这方面的文章(尽管我花了4个小时阅读这篇文章)。但非常感谢,你们的解决方案帮了我很多忙,我现在无法尝试,但我认为这会奏效
package so;
public class Main {
public static void main(String[] args) {
Students stud1 = new Students("Max", "MustermanAddress", 181242, "CS50");
Staff staff1 = new Staff("Christian", "AugustAddress");
School school = new School();
school.addPeople(stud1);
school.addPeople(staff1);
school.displayPeople();
school.removePeople(staff1);
System.out.println();
System.out.println();
school.displayPeople();
}
}
Name: Max
Address: MustermanAddress
Matriculation Number: 181242
Course Name: CS50
Name: Christian
Address: AugustAddress
Name: Max
Address: MustermanAddress
Matriculation Number: 181242
Course Name: CS50
import java.util.ArrayList;
class School {
private List<People> peopleList = new ArrayList<People>();
public void addPeople(People people) {
peopleList.add(people);
}
public void removePeople(People people) {
peopleList.remove(people);
}
public static void main(String... args) {
Student student = new Student("Name", "Address", 90, "Course");
Staff staff = new Staff("Name", "Address", 1001, "Department");
School school = new School();
school.addPeople(student);
school.addPeople(staff);
school.removePeople(student);
school.removePeople(staff);
}
}