2017-04-03 57 views
0

我是Java新手。我如何追蹤這個程序的執行?這個程序是否正確傳達了繼承,還是隻傳遞了一個沒有繼承的正常封裝?跟蹤執行和繼承java

我只是想知道如果我確實在這些類中傳遞繼承。非常感謝您的回覆。

這是第一類,TestMembers

public class TestMembers { 

    public static void main (String args []){ 

     Members Ailee=new Members("Ailee","52213116028","BRS",2,1.6,45); 
     Members Amy=new Members("Amy","52213116032","BDR",2,1.63,57); 
     Members Jack=new Members("Jack","52213116201","BRE",2,1.75,82); 

     String[] member = { "Ailee","Amy", "Jack"}; 
     JFrame frame = new JFrame(); 
     String teammembers = (String) JOptionPane.showInputDialog(frame, "Team 
     Members","Team members", 
     JOptionPane.QUESTION_MESSAGE,null,member, member[0]); 

     if (teammembers==member[0]){ 
      JOptionPane.showMessageDialog(null,Ailee,"Member Profile", 
      JOptionPane.INFORMATION_MESSAGE); 
     } else if (teammembers==member[1]){ 
      JOptionPane.showMessageDialog(null,Amy,"Member Profile", 
      JOptionPane.INFORMATION_MESSAGE); 
     } else if (teammembers==member[2]){ 
      JOptionPane.showMessageDialog(null,Jack,"Member Profile", 
      JOptionPane.INFORMATION_MESSAGE); 
     } 
     System.exit(0); 
    } 
} 

這是其它類,Person

public class Person { 

    private String name; 
    private double height; 
    private double weight; 

    public void setPersonname(String name){ 
     this.name=name; 
    } 

    public String getPersonname(){ 
     return name; 
    } 

    public void setPersonHeight(double height){ 
     this.height=height; 
    } 

    public double getPersonHeight(){ 
     return height; 
    } 

    public void setPersonWeight(double weight){ 
     this.weight=weight; 
    } 

    public double getPersonWeight(){ 
     return weight; 
    } 
} 

這是第三類,Student

public class Student extends Person{ 

    private String id_number; 
    private String course; 
    private int semester; 

    public void setStudentIdNumber(String id_number){ 
     this.id_number=id_number; 
    } 

    public String getStudentIdNumber(){ 
     return id_number; 
    } 

    public void setStudentCourse(String course){ 
     this.course=course; 
    } 

    public String getStudentCourse(){ 
     return course; 
    } 

    public void setStudentSemester(int semester){ 
     this.semester=semester; 
    } 

    public int getStudentSemester(){ 
     return semester; 
    } 
} 

這是第四類,Members

public class Members extends Student{ 

    private double bmi; 
    private String bmistatus; 

    public Members(String name,String id_number, String course, int semester, 
     double height, double weight){ 
     setPersonname(name); 
     setStudentIdNumber(id_number); 
     setStudentCourse(course); 
     setStudentSemester(semester); 
     setPersonHeight(height); 
     setPersonWeight(weight); 
    } 

    public double getMemberBMI(){ 
     bmi= getPersonWeight()/(getPersonHeight()*getPersonHeight()); 
     DecimalFormat twoDForm = new DecimalFormat("#.00"); 
     return Double.valueOf(twoDForm.format(bmi)); 
    } 

    public String BMIstatus(){ 
     if (bmi>= 18.5 & bmi <= 25.0){ 
      return bmistatus= "Optimal weight"; 
     } else if (bmi<18.5){ 
      return bmistatus="Underweight"; 
     } else if (bmi>25.0){ 
      return bmistatus="Overweight"; 
     } 
     return bmistatus; 
    } 

    public String toString() { 
     return "Name: " + getPersonname() + "\nID: " + getStudentIdNumber() 
     +"\nCourse: 
     " + getStudentCourse()+ 
     "\nSem: " + getStudentSemester()+"\nHeight: "+getPersonHeight()+" metre"+ 
     "\nWeight: "+getPersonWeight()+" kg"+"\nBMI:"+getMemberBMI()+"\nBMI   status 
     :"+BMIstatus(); 
    } 
} 
+0

在類內和類之間添加格式 –

回答

0

我認爲,繼承是 - 語法正確。從設計角度來看,這取決於更廣泛的背景。

關於軟件設計,沒有簡單的方法來學習它。您可能想要閱讀Clean Code,RefactoringDesign Patterns(按此順序)的書籍。他們會教你很多基本規則。但是,你也需要從你的日常工作中學習,這樣你才能學會如何運用這些規則。

關於追查,您可以使用一個Logger,或者乾脆System.out,例如:

private static final Logger logger = Logger.getInstance(TestMembers.class); 
... 
if (teammembers==member[0]) { 
    logger.debug("first member"); 
} 
... 

另外,我建議刪除尾隨System.exit(0);還有其他方法可以做到這一點。