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();
}
}
在類內和類之間添加格式 –