使用“instanceof”的类型转换方法

时间:2016-04-19 06:10:31

标签: java arrays class polymorphism instanceof

所以我在设置一些方法时遇到了一些问题,我一直盯着电脑屏幕看了太久。我对5种方法中的4种都有困难。最后两个方法需要调用CompSciStudent类,因此使用'instanceof',因为并非所有数组中的Student都是CompSciStudents。这两种方法需要在Student数组中向Student添加CS语言,另一种方法是检查数组中有多少CompSciStudents知道特定语言。另外两种方法处理整个数组。第一个是将测试分数添加到特定位置的特定学生,阵列中的每个学生在阵列中调用时都会有一个特定的名称。最后一种方法是获得平均测试分数。该方法是调用阵列中Student和CompSciStudents的平均测试分数,然后分别取平均值,然后取平均值。看看Class结构:

public class School {

    private Student[] theStudents;

    public School() {
        this.theStudents = new Student[] { null };// needs to start out as empty
    }

    /*
     * next two methods would allow a user to add or drop a student into the
     * student array for the school ??Also with Enroll student, should be able
     * to assign a class to the student, i.e. Calculas, history, etc??
     */
    public void enrollStudent(Student newStudent) {
        Student totalStudents[] = new Student[theStudents.length + 1];
        for (int i = 0; i < theStudents.length; i++) {
            totalStudents[i] = theStudents[i];
        }
        totalStudents[theStudents.length] = newStudent;
        theStudents = totalStudents;
    }

    public void dropStudent(Student dropStudent) {
        Student totalStudents[] = new Student[theStudents.length - 1];
        for (int i = 0; i > theStudents.length; i--) {
            totalStudents[i] = theStudents[i];
        }
        totalStudents[theStudents.length] = dropStudent;
        theStudents = totalStudents;
    }

    // add Test Score for a student
    public double addTestScore(String newStudent, double testScore) {
        testScores[posi] = testScore;
    }

    /*
     * count the number of students in a given class, not the school
     */
    public int countClassSize(String course) {
        // Need to access how the course names are stored for the school to
        // count this size.
        String courseName;
        if (this.courseName.equals(course)) {
            for (int i = 0; i < theStudents.length; i++) {
                int count = count + 1;
            }
        }
    }

    /*
     *  get average average score of the student array
     *  The student array is made up of Regular students and
     *  CompSciStudents. The average should take the average of
     *  both the average score for Students and average score of 
     *  CompSciStudents and return the average average.
     */

    public double averageAverageScore(String course) {
        double averageCourseScore;
        if(this.theStudents.equals(course)/2){
            return averageCourseScore;
        }

    }

    /*
     * add a programming language to only CS students at the school Will need to
     * use the instanceof for proper type casting
     */
    public String addProgrammingLanguage(String studentName, String programLanguage) {
        for (int i = 0; i ; i++);
        if (this.theStudents[i] instanceof CompSciStudent);
            CompSciStudent tempStudent = (CompSciStudent)this.theStudents[i];
                tempStudent.knowsLanguage(knowThisLanguage);

    }

    /*
     * Count the number of students in the school that know a certain
     * programming language, again will need to typecast properly
     */
    public int numberThatKnowLanguage(String programLanguage) {
        for();
                if(this.theStudents[i] instanceof CompSciStudent);
                CompSciStudent tempStudent = (CompSciStudent)this.theStudents[i];
                    tempStudent.learnedLanguage(knowThisLanguage);

    }

1 个答案:

答案 0 :(得分:0)

您面临的主要问题是应用程序的设计。我建议一些设计方面的改进可以帮助您解决问题。

  1. 不要将theStudents[]声明为数组,而应将其设为List<Student> students之类的列表。这将大大减少您在注册学生和放弃学生时所付出的额外努力。
  2. 使用显式方法调用为学生添加主题不应该是理想的情况。受试者应与特定学生密切相关。为了遵守设计原则,Student对象的设计方式应该是&#34;学生具有名称,具有 ID, 科目&#34;。在创建学生对象时注意添加主题。因此,这消除了添加额外方法的需要。
  3. 将学生分类为计算机科学专业的学生,​​电子学生等应该在一个单独的课程中处理,请将其称为部门。现在说为了简单起见,我们只考虑两个部门。一个是计算机科学,另一个是电子。 &#34;每个部门都有学生&#34;。根据设计原则( has-a 关系),您应该在您的部门类中创建List<Student> students(如第1点中所述),最后您的School类将有{ {1}}。这是一对多的关系。
  4. 在比较两个或更多List<Department> departments个对象时,不要忘记覆盖hashCode()equals(Object obj)方法,因为它们是必要的。
  5. 现在,您可以说&#34; Student 部门。每个School 都有学生。每个Department 都有主题&#34;。

    现在你的课程应该看起来像这样:

    Student

    注意:我并不是说这是一个完美的设计。但对于你所解释的场景来说,这是非常微妙的。同样,代码设计可能因人而异。