跟踪执行和继承java

时间:2017-04-03 13:31:24

标签: inheritance encapsulation tracing

我是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();
    }
}

1 个答案:

答案 0 :(得分:0)

我认为,继承在语法上是正确的。从设计的角度来看,它取决于更广泛的背景。

关于软件设计,没有简单的方法来学习它。您可能希望阅读Clean CodeRefactoringDesign Patterns上的图书(按此顺序)。他们会教你很多基本规则。但是,您也需要从日常工作中学习,这样您就可以学习如何应用这些规则。

关于跟踪,您可以使用Logger,或只使用System.out,例如:

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

另外,我建议删除尾随System.exit(0);还有其他方法可以做到这一点。