将人员添加到建筑物时,引用学校 Class 的最合适方式是什么?
What Would Be the Most Appropriate Way to Refer to a School Class When Adding People to a Building?
我需要在计算机科学的每个练习中应用 OOP 概念 class。练习 7 向我们介绍了 类 和对象。我们必须创建一个名为 Person 的 class 并对其进行编程,以便从 10 人的一维数组中添加、删除和显示一个人。接下来是练习 8 和 9,其中我们介绍了继承和封装,我们必须创建一个继承自 Person 的 Student class、一个继承自 Person 的 Teacher class 和一个 Principal class 将从 Person 继承。我必须能够添加、删除和显示 students/teachers 的列表,当我完成这个时,它工作得很好。
我们的下一个任务是练习 10,它向我们介绍了多态和转换。我们必须创建一个学校 class,其中包含学校名称、地址和人员列表(最多 300)。我得设计一个方法addPersonToBuilding(Person person),然后修改我之前的程序,让人在创建的时候就加入学校。我的程序(主要方法)应该只需要引用学校 class。
在创建方法 addPersonToBuilding 时,我相信我d add the student and teacher instance in the people array. I
会创建一个方法来将 student/teacher 添加到学校,并在这些方法中将 student/teacher 添加到人员中大批。 addPersonToBulding 方法将引用 add student/teacher to school 方法。每当用户选择在 main 方法中添加 student/teacher 时,将调用添加 PersonToBuliding 的方法。
public class School extends Person {
private String schoolName;
private String address;
private String name;
private School people[] = new School[300];
public School() {
this.schoolName = "Not Available";
this.address = "Not Available";
this.name = "Not Available";
}//end of School()
public School (String schoolName, String address, String prinicpalName) {
this.schoolName = schoolName;
this.address = address;
this.name = prinicpalName;
}//end of School(schoolName, address)
public School (String schoolName, String address, String prinicpalName, School[] people) {
this.schoolName = schoolName;
this.address = address;
this.name = prinicpalName;
this.people = people;
}//end of School(schoolName, address, people)
// --------- include setters and getter methods to access private data -----
// I won`t include this because that`s not really an issue I`m having with
//I`m assuming everyone, including me, knows how they are and what they do
public boolean addPersonToBuilding (PersonCopy P) {
boolean personAdded = false;
int index = 0;
return personAdded;
}//addPersonToBuilding()
// Create a method to add a student to the school
public boolean addStudentToSchool (Student student) {
boolean studentAdded = false;
int index = 0;
return studentAdded;
}//end of addStudentToSchool(student)
// Create a method to add a teacher to the school
public boolean addTeacherToSchool (Teacher teacher) {
boolean teacherAdded = false;
int index = 0;
if (addTeacherToSchool(teacher)) {
}
return teacherAdded;
}//end of addTeacherToSchool(teacher)
}//end of class School
我知道我需要做什么,但我在代码上方所做的假设没有t speak "on the right track" to me. I feel like I
m 遗漏一些细节。
给定 Teacher 和 Student 继承自 Person,您创建了以下学校 class:
public School(){
ArrayList<Person> people = new ArrayList<Person>();
public void addPeople(Person p){
people.add(p);
}
我认为 Main 函数的示例可以帮助您解决问题:
public static void main(String[] args){
School school = new School();
Teacher t1 = new Teacher();
Student s1 = new Student();
school.addPeople(t1);
school.addPeople(s1);
//Polymorphism
for(Person p: school.getPeople()){
p.getName(); /* Here the function will be
called depending if its a Teacher or Student at runtime */
}
}
我需要在计算机科学的每个练习中应用 OOP 概念 class。练习 7 向我们介绍了 类 和对象。我们必须创建一个名为 Person 的 class 并对其进行编程,以便从 10 人的一维数组中添加、删除和显示一个人。接下来是练习 8 和 9,其中我们介绍了继承和封装,我们必须创建一个继承自 Person 的 Student class、一个继承自 Person 的 Teacher class 和一个 Principal class 将从 Person 继承。我必须能够添加、删除和显示 students/teachers 的列表,当我完成这个时,它工作得很好。
我们的下一个任务是练习 10,它向我们介绍了多态和转换。我们必须创建一个学校 class,其中包含学校名称、地址和人员列表(最多 300)。我得设计一个方法addPersonToBuilding(Person person),然后修改我之前的程序,让人在创建的时候就加入学校。我的程序(主要方法)应该只需要引用学校 class。
在创建方法 addPersonToBuilding 时,我相信我d add the student and teacher instance in the people array. I
会创建一个方法来将 student/teacher 添加到学校,并在这些方法中将 student/teacher 添加到人员中大批。 addPersonToBulding 方法将引用 add student/teacher to school 方法。每当用户选择在 main 方法中添加 student/teacher 时,将调用添加 PersonToBuliding 的方法。
public class School extends Person {
private String schoolName;
private String address;
private String name;
private School people[] = new School[300];
public School() {
this.schoolName = "Not Available";
this.address = "Not Available";
this.name = "Not Available";
}//end of School()
public School (String schoolName, String address, String prinicpalName) {
this.schoolName = schoolName;
this.address = address;
this.name = prinicpalName;
}//end of School(schoolName, address)
public School (String schoolName, String address, String prinicpalName, School[] people) {
this.schoolName = schoolName;
this.address = address;
this.name = prinicpalName;
this.people = people;
}//end of School(schoolName, address, people)
// --------- include setters and getter methods to access private data -----
// I won`t include this because that`s not really an issue I`m having with
//I`m assuming everyone, including me, knows how they are and what they do
public boolean addPersonToBuilding (PersonCopy P) {
boolean personAdded = false;
int index = 0;
return personAdded;
}//addPersonToBuilding()
// Create a method to add a student to the school
public boolean addStudentToSchool (Student student) {
boolean studentAdded = false;
int index = 0;
return studentAdded;
}//end of addStudentToSchool(student)
// Create a method to add a teacher to the school
public boolean addTeacherToSchool (Teacher teacher) {
boolean teacherAdded = false;
int index = 0;
if (addTeacherToSchool(teacher)) {
}
return teacherAdded;
}//end of addTeacherToSchool(teacher)
}//end of class School
我知道我需要做什么,但我在代码上方所做的假设没有t speak "on the right track" to me. I feel like I
m 遗漏一些细节。
给定 Teacher 和 Student 继承自 Person,您创建了以下学校 class:
public School(){
ArrayList<Person> people = new ArrayList<Person>();
public void addPeople(Person p){
people.add(p);
}
我认为 Main 函数的示例可以帮助您解决问题:
public static void main(String[] args){
School school = new School();
Teacher t1 = new Teacher();
Student s1 = new Student();
school.addPeople(t1);
school.addPeople(s1);
//Polymorphism
for(Person p: school.getPeople()){
p.getName(); /* Here the function will be
called depending if its a Teacher or Student at runtime */
}
}