簡體   English   中英

如何從超類方法調用子類方法?

[英]How to call a Subclass method from a Superclass method?

我正在嘗試制作一個程序,根據學生的總分准備一份學生成績單。 如果兩個學生的總分相同,則檢查不同科目的個人分數。

在第 40 行,我試圖從超類方法的定義中調用子類方法,但出現錯誤。

import java.util.*;


class Student{
int[] ph, ch, ma;
int[] total;
int[] total_sort;

Student(){}

// length of array and marks are received via constructor.
Student(int x, int[] p, int[] c, int[] m){

    // an array of p,c,m along with 2 arrays of total marks is made.
    total = new int[x];
    total_sort = new int[x];
    ph = new int[x];
    ch = new int[x];
    ma = new int[x];
    for(int i = 0; i < x; i++){ 
        ph[i] = p[i];
        ch[i] = c[i];
        ma[i] = m[i];
        total[i] = (p[i] + c[i] + m[i]);
        total_sort[i] = total[i];
    }

}

// sorts the array accoring to the total marks
void Sort(){

    for(int f = 0; f < total.length; f++){

        if(total[f] > total[f+1])
            total_sort[f] = total[f];

        // checks for higher maths marks.
        else if(total[f] == total[f+1]){
            int m = Stud_new.mSort(f);
            total_sort[f] = total[m];
        }   

    }
}

/* returns the index from original total array so as to identify the         students' name.
 * marks from sorted array are compared with original array to find the   index of marks in originial array .
 * this index is used to find the students' name.
 */
int GetList(int a){


        for(int j = 0; j < total.length; j++){

            if(total[j] == a)
                return j;
        }


    return -1 ;

}

}

class Stud_new extends Student{

int cSort(int ci){
    if(ch[ci] > ch[ci + 1])
        return ci;

    else if(ch[ci] < ch[ci + 1])
        return (ci + 1);

    //else if(ph[pi] == ph[pi + 1])
        //csort(pi);
}

int pSort(int pi){

    if(ph[pi] > ph[pi + 1])
        return pi;

    else if(ph[pi] < ph[pi + 1])
        return (pi + 1);

    else if(ph[pi] == ph[pi + 1])
        return(cSort(pi));
}

int mSort(int mi){

    if(ma[mi] > ma[mi + 1])
        return mi;

    else if(ma[mi] < ma[mi + 1])
        return (mi + 1);

    // checks higher phy marks
    else if(ma[mi] == ma[mi + 1])
        return(pSort(mi));
}

}

class Mlist{

public static void main(String args[]){

    // initializes the names and marks.
    String[] name = {"foo", "bar", "baz"};
    int[] phy = {80,112,100};
    int[] chem = {100,120,88};
    int[] maths = {40, 68,60};

    Student stud = new Student(name.length, phy, chem, maths);

    System.out.println("Name\tPhysics\tChemistry\tMaths\tName");

    // prints the merit list
    for(int i = 0; i < name.length; i++){

        // index of name is received by a function call

        /* STUD.TOTAL_SORT[i] IS STUDENTS' MARKS IN SORTED MANNER.
         * THIS IS PASSED AS AN ARGUMENT TO GETLIST METHOD WHICH USES LINEAR SEARCH
         * TO FIND THE INDEX IN THE ARRAY OF NAMES FOR THE STUDENT WHOSE MARKS IS PASSES
         * THE FUNCTION RETURNS INT WHICH IS THE INDEX FOR NAME[] ARRAY. and others all well.
         * HERE STUD.TOTAL_SORT[I] IS AN ARGUMENT TO THE FUNCTION STUD.GETLIST() WHICH RETURNS AN INT            
         */
          System.out.println(name[stud.GetList(stud.total_sort[i])]+"\t"+phy[stud.GetList(stud.total_sort[i])]+"\t"+chem[stud.GetList(stud.total_sort[i])]+"\t"+maths[stud.GetList(stud.total_sort[i])]+"\t"+stud.total_sort[i]);
    }


}

}

您可能正在尋找工廠方法模式式的解決方案。 詳情請自行google,不過簡單來說,你希望Student類是抽象的,添加抽象mSort方法,然后在Stud_new類中實現該方法。 然后您必須實例化Stud_new對象。

提示:你可以這樣做:

Student stud = new Stud_new(name.length, phy, chem, maths);

這遠非完美的解決方案,但此解決方案允許您從超類中的子類調用方法,這正是您所要求的。

從超類方法調用子類方法是不可能的,也是錯誤的。

這違反了繼承規則。

超類代表一種共同的行為,而子類具有對其超類不可見的特定行為。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM