我想从一个合成的类中调用一个方法

时间:2015-11-01 09:33:15

标签: polymorphism

所以我有一个主要的实验室课程,一个班级实验室和一个班级学生 在学生中我有一个名为getGrade()的方法,在Lab类中我有一个Object Student Student [] st的数组;在我的主要部分,我有一系列Lab Lab []实验室; 并且我想获得通过的学生数量,是否可以使用Lab数组从LaboratoryCourse调用方法getGrade(),而无需在类Lab中创建一个方法来收集那些学生。 我想到的是像lab [i] .st [a] .getGrade();

    public class LaboratoryCourse {
//Menu
public static void listaEpilogwn(){
    System.out.println("\t1.Eisagwgh Mathith");
    System.out.println("\t2.Diagrafh Mathith");
    System.out.println("\t3.Anazhthsh Mathith");
    System.out.println("\t4.Ektupwsh parousiologiou");
    System.out.println("\t5.telos");
}//End of menu

public static void main(String[] args){
    Lab[] lab=new Lab[10];
    int choice;
    int i=0;
    int parakolouthoun=0;
    do{ 
        System.out.println("se poio tmhma thelete na kanete allages");
        i=UserInput.getInteger();
        listaEpilogwn();
        choice=UserInput.getInteger();
        switch(choice){
            case 1: lab[i].Insert();
            parakolouthoun++;
                break;
            case 2: 
                System.out.println("apo poia thesi thelete na diagrapsete");
                lab[i].Delete(UserInput.getInteger());
                parakolouthoun--;
                break;
            case 3:
                System.out.println("dwste am tou foithth pou anazhthtai");
                lab[i].Search(UserInput.getInteger());
                break;
            case 4:lab[i].Type();
                break;
            case 5:System.out.println("telos");
                break;
            default:
                System.out.println("den dwsate ekgurei timh\ndwste timh (1-5)");
        }
    }while(choice!=5);//End of changes in labs
    for(Lab lab1: lab){
        for(Student st1: st){

        }
    }

}//End of main

} //程序结束

   public class Lab {
private Student[] st;
static boolean flag=true;
static int temp;
static int i=0;

public Lab() {
    this.st = new Student[30]; //edwsa mia logikh timh gia ena tmhma 
                               //me skeptiko oti den tha mpoun
                               //perissotero apo 30 foithtes
}

/**
 *
 * @param st
 */
public  void Insert(){
  if(i<30){//elenxos tou i 
    if(flag){//elenxo me mia flag an exei diagrafei kapoios ap to tmhma wste na sumplhrwthei auth h thesi
        st[i]=new Student();                            //eisagwgh stoixeiwn foithth
     System.out.println("dwste AM");                    //thesi tousthn prwth  pinaka
    st[i].setAM(UserInput.getInteger());         
    System.out.println("dwste onoma");
    st[i].setName(UserInput.getString());
    System.out.println("dwste epitheto");
    st[i].setSurname(UserInput.getString());
    System.out.println("dwste hlikia");
    st[i].setAge(UserInput.getInteger());
    System.out.println("dwste fulo");
    st[i].setSex(UserInput.getChar());
    System.out.println("dwste apousies ");
    st[i].setApousies(UserInput.getInteger());
    System.out.println("dwste bathmo");
    st[i].setGrade(UserInput.getDouble());
    i++;
    }else
    st[temp]=new Student();    
     System.out.println("dwste AM");            //dinw nees times sta stoixeia opou exei afairethei o 
    st[temp].setAM(UserInput.getInteger());        //prohgoumenos foithths 
    System.out.println("dwste onoma");
    st[temp].setName(UserInput.getString());
    System.out.println("dwste epitheto");
    st[temp].setSurname(UserInput.getString());
    System.out.println("dwste hlikia");
    st[temp].setAge(UserInput.getInteger());
    System.out.println("dwste fulo");
    st[temp].setSex(UserInput.getChar());
    System.out.println("dwste apousies ");
    st[temp].setApousies(UserInput.getInteger());
    System.out.println("dwste bathmo");
    st[temp].setGrade(UserInput.getDouble());
    }else
        System.out.println("array out of bounds");
}

public void Delete(int i){
    st[i].setAM(-1);          //dinw kenes times sths metablhtes
    st[i].setName(" ");
    st[i].setSurname(" ");
    st[i].setAge(-1);
    char a = 0;
    st[i].setSex(a);
    st[i].setApousies(-1);
    st[i].setGrade(-1.0);
    temp=i;           //dinw se mia temp thn thesh opou diagrafthke o foithths 
    flag=false;
}
public int Search(int key){
    if(i>-1){


      for (int j=0; j<st.length; j++)
           if (st[j].getAM()==key)
                return j; 
      return -1;
    }else
        System.out.println("arra.out.of bounds");
    return -1;
 }

/**
 *
 */
public void Type(){
    for (Student st1 : st) {
        System.out.println(st);

    }

}

}

   public class Student {
private int AM;
private String name;
private String surname;
private double age;
private char sex;
private int apousies;
private double grade;

Student(){}
Student(int am,String n,String sn,double a,char s,int ap,double gr){
    AM=am;
    name=n;
    surname=sn;
    age=a;
    sex=s;
    apousies=ap;
    grade=gr;
}
//methodoi get
public int getAM(){
    return AM;
}
public String getName(){
    return name;
}
public String getSurname(){
    return surname;
}
public double getAge(){
    return age;
}
public char getSex(){
    return sex;
}
public int getApousies(){
    return apousies;
}
public double getGrade(){
    return grade;
}
//methodoi set
public void setAM(int am){
    AM=am;
}
public void setName(String n){
    name=n;
}
public void setSurname(String sn){
    surname=sn;
}
public void setAge(int a){
    age=a;
}
public void setSex(char s){
    sex=s;
}
public void setApousies(int ap){
    apousies=ap;
}
public void setGrade(double gr){
    grade=gr;
}
@Override
public String toString(){
    return "Student :"+name+" "+surname+"\nAM :"+AM+"age :"+age+"\nsex :"+sex+"\napousies :"+apousies+"\nbathmos :"+grade;
}

}

1 个答案:

答案 0 :(得分:0)

除非是静态类,否则必须创建类对象的实例来调用方法。否则,您将null对象引用运行时错误。