2015-11-01 20 views
-1

所以我有一個主類LaboratoryCourse,一個類實驗室和一個班學生 在學生我有一個名爲getGrade()的方法,在Lab類中我有一個數組對象學生學生[] st;並在我的主要我有一個Lab Lab實驗室; 我想獲得通過的學生人數,是否可以使用Lab數組從實驗室數據庫中調用方法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; 
} 

}

+0

請寄出代碼,爲一個最小的工作示例。 – skypjack

回答

0

你必須創建該類對象的實例調用方法,除非它是靜態的類。否則,你將空對象引用運行時錯誤。